remove last comma from string

I have a forloop that adds a $var and , to $string

how to remove last comma


example

3,4,5,6,2,
LVL 1
rgb192Asked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
$string = rtrim($string, ",");

Open in new window

0
 
Ray PaseurConnect With a Mentor Commented:
The advantage of using rtrim() is that if you have empty positions at the end of the array and wind up with something like this 3,4,5,,, it will declop all of the commas at the end.  Thus it is better than something like substr($str,0,-1).

But I have another suggestion for you.  Why use an iterator at all?  Why not do something like this...

$arr = array(3,4,5,6,2);
$str = implode(',', $arr);

HTH, ~Ray
0
 
rgb192Author Commented:
thanks both delete the last ,
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.