Solved

remove last comma from string

Posted on 2012-12-27
3
500 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Having trouble getting Imagick working with php on windows. 2 48
Make check boxes work 8 44
PHP encrypted string and passing to a ASP Page 12 42
Echo'd values in dropdowns 6 29
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 count occurrences of each item in an array.

756 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