寻找一种方法从字符串中删除一个尾随逗号只有当有一个PHP [重复]

寻找一种方法从字符串中删除一个尾随逗号只有当有一个PHP [重复]

问题描述:

Possible Duplicate:
PHP - Remove last character if it’s a period?

I am trying to strip a trailing comma from a string. I do it like this:

$_POST['city_id_list'] //looks like 1,4,213,
$trimmed = substr(trim($_POST['city_id_list']),0,-1);

But what if the last character is not a comma?

java has something like:

str.replaceAll(" ,$", "")

I assume there is a way to do this with PHP. What is the best way to do it?

Take a look at the rtrim function:

$trimmed = rtrim($_POST['city_id_list'], ',');