Contrary to the question asked, rtrim() will remove any number of characters, listed in the second argument, from the end of the string. In case you expect just a single comma, the following code would do:

$newarraynama = rtrim($arraynama, ",");

But in my case I had 2 characters, a comma and a space, so I had to change to

$newarraynama = rtrim($arraynama, " ,");

and now it would remove all commas and spaces from the end of the string, returning a, b, c, d, e either from a, b, c, d, e,, a, b, c, d, e,,,, a, b, c, d, e, or a, b, c, d, e , ,, , ,

But in case there could be multiple commas but you need to remove only the last one, then rtrim() shouldn't be used at all - see other answers for the solution that directly answers the question.

However, rtrim() could be a good choice if you don't know whether the extra character could be present or not. Unlike substr-based solutions it will return a, b, c, d, e from a, b, c, d, e

Answer from anon on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-program-to-remove-last-character-from-the-string
PHP Program to Remove Last Character from the String - GeeksforGeeks
July 23, 2025 - The substr() function in PHP can be used to return a part of a string. By specifying the start and length parameters, we can effectively remove the last character. The substr() function is used with the start parameter as 0 (beginning of the ...
🌐
FlatCoding
flatcoding.com › home › how to remove the last character from a string in php
How to Remove the Last Character from a String in PHP - FlatCoding
August 23, 2025 - You can remove the last character from a PHP string using four methods: substr_replace, substr, mb_substr, and rtrim. Here’s an example..
People also ask

What if I want to remove the last character only when it matches a pattern?
Use preg_replace() with a regular expression like /character$/ to match it safely.
🌐
flatcoding.com
flatcoding.com › home › how to remove the last character from a string in php
How to Remove the Last Character from a String in PHP - FlatCoding
Can I remove only slashes or commas from the end?
Yes. Use rtrim($string, "/,") to remove slashes or commas from the end only.
🌐
flatcoding.com
flatcoding.com › home › how to remove the last character from a string in php
How to Remove the Last Character from a String in PHP - FlatCoding
How do I handle strings with emojis or accents?
Use mb_substr() and mb_strlen() to avoid breaking multibyte characters.
🌐
flatcoding.com
flatcoding.com › home › how to remove the last character from a string in php
How to Remove the Last Character from a String in PHP - FlatCoding
🌐
W3Docs
w3docs.com › php
How to Remove the Last Character from a String in PHP | W3Docs
To remove the last character, replace it with an empty string: ... <‌?php $string = "Hello World!"; echo "Given string: " . $string . "\n"; echo "Updated string: " . substr_replace($string, "", -1) . "\n"; ?> ... The rtrim function removes ...
🌐
DEV Community
dev.to › lavary › how-to-remove-the-last-character-from-a-string-in-php-31ci
How to remove the last character from a string in PHP - DEV Community
February 6, 2023 - PHP rtrim() accepts two arguments: the input string and characters you wish to be stripped from the end of the input string. For instance, to remove the last comma in a comma-separated list:
🌐
DEV Community
dev.to › patricia1988hernandez2 › remove-the-last-char-from-a-php-string-39eo
Remove the Last Char from a PHP String - DEV Community
April 17, 2025 - $offset to select the started index from left to right and if it take a negative value that means it will start from the right. And the final is an optional parameter with $length. Let's take an example. <?php $string = "My Office behind the Door.."; $replace = ""; $offset = -1; echo substr_replace( $string, $replace, $offset ); // The output: My Office behind the Door. ?> As you saw, the value was printed is the full sentence but deleted the last dot from the string.
🌐
Hellonitish
hellonitish.com › blog › remove-first-or-last-character-from-a-string-in-php
Hello Nitish • How Can I Remove the First or Last Character from a String in PHP?
November 4, 2025 - The PHP substr() function returns the part of string specified by the start and length parameters. If you want to remove characters from the end of string, you can set the value of start to 0 and the value of length to a negative number. You can set the value of length to -1 in order to remove ...
Find elsewhere
🌐
Java2Blog
java2blog.com › home › php › remove last character from string in php
Remove Last Character from String in PHP [5 ways] - Java2Blog
December 12, 2022 - To remove last character from String in PHP, use of the substr() function and pass a negative length value as argument.
🌐
Delft Stack
delftstack.com › home › howto › php › php remove last character from string
How to Remove the Last Character From a String in PHP | Delft Stack
March 13, 2025 - Another effective way to remove the last character from a string in PHP is by using the substr() function. This function returns a part of a string based on specified start and length parameters.
🌐
TecAdmin
tecadmin.net › remove-last-character-from-string-in-php
Remove Last Character from String in PHP (4 Methods) – TecAdmin
April 26, 2025 - Don’t Miss – Check If String Contains a Sub String in PHP · You can use the PHP substr_replace() function to remove the last character from a string in PHP.
🌐
Lage
lage.us › PHP-Remove-Last-Character-From-String.html
Remove the Last Character From a PHP String
Lage.us PHP Snippets | HTML Snippets | Javascript Snippets | CSS Snippets · Removes the last character from a string, in the example below it is a comma.
🌐
sebhastian
sebhastian.com › php-remove-last-character-from-string
How to remove the last character from a PHP string | sebhastian
December 9, 2022 - Another way to remove the last character from a string in PHP is to use the rtrim() function.
🌐
If Not True Then False
if-not-true-then-false.com › 2010 › php-remove-last-character-from-string
PHP: Remove Last Character from String &#8211; substr / substr_replace / rtrim :: If Not True Then False
October 3, 2010 - Method 1 – PHP: Remove Last Character from String using substr and mb_substr substr and mb_substr commands usage substr($string, 0, -1); mb_substr($string, 0, -1); substr and mb_substr example: $string = "This is test string.."; echo $string .
🌐
Extreme Web Designs
theextremewebdesigns.com › blog › php-remove-last-character-how-to-remove-last-character-from-string-examples
How To Remove Last Character From String Using PHP – Examples |
/*Example1:=====================================================================*/ echo 'Example 1: Removing the Last character from string using PHP<br />'; echo "Before using rtrim: <br />"; //Sample String $my_string = 'Hello! This is my random string'; //Echo the sample string echo $my_string .
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-remove-the-last-character-from-a-string-in-php.php
How to Remove the Last Character from a String in PHP
PHP Array Functions PHP String Functions PHP File System Functions PHP Date/Time Functions PHP Calendar Functions PHP MySQLi Functions PHP Filters PHP Error Levels ... You can simply use the rtrim() function to remove the last character from ...
🌐
DaniWeb
daniweb.com › programming › web-development › threads › 543432 › how-do-i-remove-the-last-character-without-breaking-the-string
php - How do I remove the last character without ... [SOLVED] | DaniWeb
Use string slicing: your_string[:-1] — this removes the last character without breaking the string. ... Reverend Jim 6,565 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster 1 Year Ago · That's how you do it in Python.
🌐
PrintMyFonts
askingbox.com › question › php-cut-first-and-last-character-from-string
PHP: Cut first and last Character from String
You can use the PHP function substr() for cutting off your string at the front and at the back. ... In this example, we are transforming the string "abcde" into "bcd". You can remove the first and the last character by passing 1 as second parameter and -1 as third parameter to substr().