0

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?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
randy
  • 1,685
  • 3
  • 34
  • 74

1 Answers1

4

Take a look at the rtrim function:

$trimmed = rtrim($_POST['city_id_list'], ',');
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292