$string = rtrim($string, ',');
Docs for rtrim here
$string = rtrim($string, ',');
Docs for rtrim here
This is a classic question, with two solutions. If you want to remove exactly one comma, which may or may not be there, use:
if (substr($string, -1, 1) == ',')
{
$string = substr($string, 0, -1);
}
If you want to remove all commas from the end of a line use the simpler:
$string = rtrim($string, ',');
The rtrim function (and corresponding ltrim for left trim) is very useful as you can specify a range of characters to remove, i.e. to remove commas and trailing whitespace you would write:
$string = rtrim($string, ", \t\n");
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?
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.
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
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