Trimming the end of a string if it contains a certain character?
Let's say I have a string like this: asd开发者_JAVA技巧f, asdf, asdf,
. There is a comma and a space at the end. I want to trim that off only if it is a comma and a space. If the string looks like this asdf, asdf, asdf
then I do not want it to trim off the last two characters. How can I do this?
rtrim($str, ", ");
That will trim all of the spaces and commas from the right of the string. It is faster than a regexp.
$str = preg_replace('~, $~', '', $str);
精彩评论