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
🌐
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..
🌐
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 ...
People also ask

How to remove the last character of a string in PHP?
1. Use substr()
$text = "Hello!";
$result = substr($text, 0, -1);
echo $result; // Hello

substr() cuts part of a string. The 0 means start at the first character. The -1 tells PHP to stop before the last character.

2. Use rtrim()
$text = "Hello!";
$result = rtrim($text, "!");
echo $result; // Hello

rtrim() removes chosen characters from the end. Here it removes the !.

3. Use mb_substr() (for UTF-8)
$text = "مرحبا!";
$result = mb_substr($text, 0, -1, "UTF-8");
echo $result; // مرحبا

mb_substr() is safe for Unicode text. It works like substr() but supports multi-byte characters such as Arabic.

🌐
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
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
🌐
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) .
🌐
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:
🌐
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.
🌐
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
🌐
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 - That means, we need only to return the need part without the last Character. Let's see that in the following PHP code. <?php $string = "Am I Doing That ?"; echo mb_substr( $string , 0, -1 ); // Am I Doing That ?> In this article, you understood what is substr_replace function and how to use it to replace a string from a sentence. Also you learned how to remove the last char from a PHP string using two predefined PHP functions.
🌐
Decodingweb
decodingweb.dev › php-remove-last-character-from-string
How to remove the last character from a string in PHP
September 22, 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:
🌐
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.
🌐
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.
🌐
PrintMyFonts
askingbox.com › question › php-remove-last-character-from-string
PHP: Remove last Character from String
June 26, 2015 - You can use the PHP function substr() for this purpose. In order to remove the last character from the string, you have to pass 0 as first parameter and -1 as second parameter.
🌐
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 ...
🌐
Reactgo
reactgo.com › home › remove the last character of a string in php
Remove the last character of a string in PHP | Reactgo
June 10, 2023 - To remove the last character of the string, we can use the built-in substr() function in PHP. The substr() function accepts three arguments, the first one is string, the second is start position, third is length.
🌐
PrintMyFonts
askingbox.com › question › php-cut-first-and-last-character-from-string
PHP: Cut first and last Character from String
... 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(). The second parameter is determining at which position the resulting substring should start.
🌐
PHP
php.net › manual › en › function.rtrim.php
PHP: rtrim - Manual
This shows how rtrim works when using the optional charlist parameter: rtrim reads a character, one at a time, from the optional charlist parameter and compares it to the end of the str string. If the characters match, it trims it off and starts over again, looking at the "new" last character in the str string and compares it to the first character in the charlist again.
🌐
Snipplr
snipplr.com › view › 69995 › remove-last-character-from-string-in-php
Remove Last Character from String in PHP - PHP Snipplr Social Repository
Remove / Replace - href / anchor /link - in html /string - preg_replace - php - regex - regular expression ... This is a very common PHP question of HOW TO remove last character from string in PHP.
🌐
Tutorialio
tutorialio.com › remove-first-or-last-character-from-a-string-in-php
How Can I Remove the First or Last Character from a String ...
April 1, 2022 - Get the Full URL or Query String of Current Page in JavaScript · How Can I Read a Large File Line by Line in PHP? Read these top five reasons why you should hire me.