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
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?
How do I handle strings with emojis or accents?
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.
I think that you're missing to assign the variable back after the trim:
$s = '"india","usa","uk",';
$s = rtrim($s, ',');
// prints "india","usa","uk"
print $s;
Demo
Try before buy
try this substr() or mb_substr()
substr($string, 0, -1);
mb_substr($string, 0, -1);
or check this link
rtrim($string, ","); removes a comma at the end of the string.
PHP.net rtrim
trim($string, ","); removes a comma at the beginning and end of a string.
PHP.net trim
You can also use implode if you're working with arrays:
PHP.net implode
Example from php.net:
<?php
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone
// Empty string when using an empty array:
var_dump(implode('hello', array())); // string(0) ""
?>
echo substr($string, 0, strlen($string)-1);
- substr
- strlen