Whether you’re working with a string created from a database query or array, here’s a cool trick to strip the last character (comma) of a comma separated list of values using PHP.
Imagine any scenario where you might like to print a comma-separated list of values. You might print all days in a month and separate them with commas. You might select all tags in your blog (like del.icio.us) and separate them with commas. Or you might loop through all checkboxes someone has selected in a form and separate them with commas. There are plenty of scenarios. The issue is this: if you use any kind of automated function or loop to print the values, which you should for efficiency’s sake, you will end up with an extra comma at the end of your output.
Here’s an example. In my blog I have several categories to organize the articles I write. For some reason, I might decide I want use a database query to print a list of the categories on one line, separated by a comma. The string might look like something like this:
ASP, ASP Programming, CSS, Information Technology, JavaScript Programming,
The logic behind this output is, for each record in the database, print the record followed by a comma and space.
PHP has a function called substr() which makes it easy to remove characters from the end of a string. To print my string without the trailing comma, all I have to do is use this function. My code would be:
<?php
$string = "ASP, ASP Programming, CSS, Information Technology, JavaScript Programming, "; // Let’s assume this string was created from a database query or loop
$newstring = substr($string, 0, -2);
echo $newstring;
// Prints ASP, ASP Programming, CSS, Information Technology, JavaScript Programming
?>
The substr() function takes 3 parameters—the string to manipulate, the character to start with, and the character to end with. In my example, I took my original comma separated string (with the extra comma) and passed the starting character ‘0’ and the ending character ‘-2’. ‘0’ simply means the beginning of the string. ‘-2’ means end the string 2 characters before it’s really finished. Why 2 characters and not one, if all we’re trying to do is strip the comma? The answer is that there is a space and comma at the end of the string that need to be stripped (i.e. ", ").
You must be logged in to post a comment.
April 30th, 2006 at 12:44 pm
You can also use rtrim().
The second, optional parameter of rtrim() is a character list. To use your example:
$string = “ASP, ASP Programming, CSS, Information Technology, JavaScript Programming, “;
$newstring=rtrim($string,’, ‘);
Printing or echo-ing $newstring would give you the same result.
May 1st, 2006 at 2:04 pm
You can also avoid the problem entirely by using implode(), so you don’t have the superfluous delimiters in the first place. :-)
March 8th, 2007 at 4:14 pm
Hi nice example, but I’m looking for the best way to find whether the sting include space at the end, if yes then return to false, no need to delete. Is there any way except regular expression?
March 20th, 2007 at 2:30 pm
Zaur,
You can check the last character in the string, as in the code below:
$st = “abcde “;
$len = strlen($st);
st[$len-1] == ‘ ‘ ? return false : return true;
March 20th, 2007 at 3:24 pm
Zaur:
The trim() function removes leading and trailing whitespaces of a string.
April 5th, 2007 at 10:34 am
$outgoing = substr_replace($incoming,”",-1);
April 10th, 2007 at 1:20 pm
How would I go about checking the last 4 characters of a string? For example, if I wanted to change this string “hello!!!!” to be “hello”, only when there are 4 exclamations.
June 20th, 2007 at 8:08 pm
I am trying to make the html input page take an amount that has commas and remove them and still spit out the correct amount. it only works if I leave out the commas. this is my code and I added your code to mine and I dont see a change in the results.
June 20th, 2007 at 9:25 pm
pRojex-
Do you have a URL we could look at? I (or another user) might be able to help.
- Chris