The rtrim() returns a value and does not modify the original value. Please use it this way:

$xcount = rtrim($xcount, ",");

From the rtrim():

This function returns a string with whitespace (or other characters) stripped from the end of str.

Answer from Praveen Kumar Purushothaman on Stack Overflow
🌐
PHP
php.net › manual › en › function.rtrim.php
PHP: rtrim - Manual
On the recurring subject of string-stripping instead of character-stripping rtrim() implementations... the simplest (with a caveat) is probably the basename() function. It has a second parameter that functions as a right-trim using whole strings: <?php echo basename('MooFoo', 'Foo'); ?> ...outputs 'Moo'. Since it also strips anything that looks like a directory, it's not quite identical with hacking a string off the end: <?php echo basename('Zoo/MooFoo', 'Foo'); ?> ...still outputs 'Moo'.
Discussions

PHP rtrim ".php" - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. I want to remove ".php" from the end of a string if it exists. Consider this: Copy$filename = 'index'; rtrim($filename,".php");//returns "index" ... More on stackoverflow.com
🌐 stackoverflow.com
Problem with rtrim function
There was an error while loading. Please reload this page · But I expected this output instead: More on github.com
🌐 github.com
3
December 25, 2024
PHP rtrim function trim extra character? Why? - Stack Overflow
Q2. Does it depend on php version? No · You just need to use echo rtrim('4dbb3dca&', '&'); not the & because 2nd parameter of rtrim works like below- More on stackoverflow.com
🌐 stackoverflow.com
Can someone explain why rtrim() dosen't work like I wish?
You may want strtok() instead. echo strtok($myteststring, '#'); More on reddit.com
🌐 r/PHPhelp
6
1
March 15, 2017
🌐
Stack Overflow
stackoverflow.com › questions › 46687735 › rtrim-not-working-string-with-html-elements › 46687851
php - rtrim not working string with html elements - Stack Overflow
you have to change your string like this, then it will work, it does not work because your string is inappropriate: $str = "<a id='AccountDocument_11' href='/view/id/11'>Picture of Collateral</a> [2017-04-01],"; echo rtrim($str,","); ... The ...
🌐
W3Schools
w3schools.com › php › func_string_rtrim.asp
PHP rtrim() Function
<!DOCTYPE html> <html> <body> Without rtrim: Hello World! <br>With rtrim: Hello World! </body> </html> ... <?php $str = "Hello World!\n\n\n"; echo "Without rtrim: " . $str; echo "<br>"; echo "With rtrim: " .
🌐
Texelate
texelate.co.uk › blog › trim-ltrim-and-rtrim-not-working-right-in-php
trim, ltrim and rtrim not working right in PHP? | Texelate
March 17, 2018 - // Removes from the beginning of a string function lStringTrim($string, $trim) { if (mb_substr($string, 0, mb_strlen($trim)) == $trim) { $string = mb_substr($string, mb_strlen($trim)); } return $string; } // Remove from the end of a string function rStringTrim($string, $trim) { if (mb_substr($str, -mb_strlen($trim)) == $trim) { return mb_substr($string, 0, -strlen($trim)); } return $string; } echo rtrim('...helloxhellohello', 'hello'); // Outputs '...hellox', not '...helloxhello' echo lStringTrim('Foobar', 'Foo'); // Outputs 'bar' echo rStringTrim('Helloo', 'o'); // Outputs 'Hello'
🌐
PHPBuilder
board.phpbuilder.com › d › 10302347-resolved-rtrim-not-working
[Resolved] rtrim() not working - PHPBuilder Forums
June 13, 2005 - i want to cut an extra comma off the end of a returned string so i have this, but it's not working. what am i doing wrong? thanks. $one = "one, "; $two = "t...
🌐
GitHub
github.com › php › php-src › issues › 17261
Problem with rtrim function · Issue #17261 · php/php-src
December 25, 2024 - php / php-src Public · Notifications · You must be signed in to change notification settings · Fork 8.1k · Star 40.2k · New issueCopy link · New issueCopy link · Closed as not planned · Closed as not planned · Problem with rtrim function#17261 · Copy link · Labels ·
Author: php
Find elsewhere
🌐
PHP Freaks
forums.phpfreaks.com › php coding › php coding help
trim(), ltrim() and rtrim() not working - PHP Coding Help - PHP Freaks
April 16, 2007 - I am trying to write a database (MySQl) clean-up script that ltrims all of the values in a certain field. But, ltrim leaves the whitespace on the returned string (read from MySQL character fields). trim exhibits the same behavior. Code: $arrFields = array('FNAME', 'LNAME', 'ADDRESS'. 'EMail', 'Ph...
🌐
w3resource
w3resource.com › php › function-reference › rtrim.php
PHP rtrim() function - w3resource
October 4, 2024 - The rtrim() function is used to remove the whitespaces and other predefined characters from the right side of a string. ... Returns the modified string. Value Type: String. ... <?php $text1="w3resource.com "; $text2="w3resource.com\t\t"; $text3="\t\tw3resource.com\x0A"; $text4="Good Morning"; $trimmed_text=rtrim($text1); var_dump($trimmed_text); echo '<br>'; $trimmed_text=rtrim($text2); var_dump($trimmed_text); echo '<br>'; $trimmed_text=rtrim($text3); var_dump($trimmed_text); echo '<br>'; $trimmed_text=rtrim($text1,'w3'); var_dump($trimmed_text); echo '<br>'; $trimmed_text=rtrim($text4,"ng"); var_dump($trimmed_text); ?>
🌐
Stack Overflow
stackoverflow.com › questions › 69261183 › replacing-characters-at-start-end-of-string-with-phps-ltrim-rtrim
trim - Replacing characters at start/end of string with PHP's ltrim/rtrim - Stack Overflow
if you're trimming the same characters from the beginning and end, just use trim() instead of rtrim() and ltrim(). ... As for why it's not working, there must be something else after the spaces and hyphens when you call get_the_title().
🌐
FlatCoding
flatcoding.com › home › php rtrim function: remove trailing characters or whitespace
PHP rtrim Function: Remove Trailing Characters or Whitespace - FlatCoding
May 19, 2025 - It only trims the end, not the start or middle. When you call rtrim() with just one argument, it removes whitespace from the end of the string. This includes: ... PHP leaves the text untouched except for the trailing spaces. You can pass a second argument to tell rtrim() what to remove.