1

I am creating comma seperated list in PHP, however I can not work out how to remove trailing commas from a string - for examples I may generate a list that looks like this,

RefundApplicationForm_(1)7.pdf,Mackintosh_-Shaker-_Suite_1514.pdf,,,,

I have tried,

str_replace($list, ",", "");

but that removes all the commas I only want to remove the last comma and any commas that have no value between them.

I am making the list by doing the following,

$list .= $value.",";

Udders
  • 67
  • 2
  • 9

2 Answers2

8

Use this

$list = rtrim($list, ',');
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
0

$list = substr($str, 0, -1);
personally I prefer the other way around: start with a comma and remove it with a
$list = substr($str, 1);

HamZa
  • 14,671
  • 11
  • 54
  • 75
Walter81
  • 493
  • 2
  • 8
  • 21