开发者

Trimming the end of a string if it contains a certain character?

开发者 https://www.devze.com 2023-02-19 21:28 出处:网络
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 thi

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消