Solved

remove last comma from string

Posted on 2012-12-27
3
498 Views
Last Modified: 2012-12-27
I have a forloop that adds a $var and , to $string

how to remove last comma


example

3,4,5,6,2,
0
Comment
Question by:rgb192
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 38725406
$string = rtrim($string, ",");

Open in new window

0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 38725481
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
 

Author Closing Comment

by:rgb192
ID: 38725765
thanks both delete the last ,
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question