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
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
You can use substr:
echo substr('a,b,c,d,e,', 0, -1);
# => 'a,b,c,d,e'
This isolates the string from the start upto and including the second last character. In other words, it isolates the whole string except the last character.
In case the last character could be multi-byte, then mb_substr() should be used instead.
How to remove the last character of a string in PHP?
$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.
$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.
What if I want to remove the last character only when it matches a pattern?
Can I remove only slashes or commas from the end?
Some options...
1) $string = preg_replace('/s$/', '', $string); // slight modification to yours which limits to trailing 's'
2) $string = rtrim($string, 's'); // note: will remove 1+
3) $string = substr($string, -1) === 's' ? substr($string, 0, -1) : $string;
4) $string = $string[-1] === 's' ? substr($string, 0, -1) : $string; // php 7.1+ only
You could use substr() to check the last letter before to remove it :
if (substr($string,-1) == 's') // get/check the last letter
$string = substr($string, 0, -1); // remove last letter