Skip to content
Advertisement

How do I remove a comma off the end of a string?

I want to remove the comma off the end of a string. As it is now I am using

$string = substr($string,0,-1);

but that only removes the last character of the string. I am adding the string dynamically, so sometimes there is no comma at the end of the string. How can I have PHP remove the comma off the end of the string if there is one at the end of it?

Advertisement

Answer

$string = rtrim($string, ',');

Docs for rtrim here

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement