• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 523
  • Last Modified:

remove last comma from string

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

how to remove last comma


2 Solutions
käµfm³d 👽Commented:
$string = rtrim($string, ",");

Open in new window

Ray PaseurCommented:
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
rgb192Author Commented:
thanks both delete the last ,
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.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now