Use PHP to strip the last character in a string

Saturday, April 22nd, 2006 by Chris Jason
9 Comments

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.

When would I need to strip the last character in a string?

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.

How do I strip the last character using PHP?

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. ", ").

Further reading

Article Comments

  • Posted by: tiffany | April 30th, 2006

    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.

  • Posted by: Chris Shiflett | May 1st, 2006

    You can also avoid the problem entirely by using implode(), so you don’t have the superfluous delimiters in the first place. :-)

  • Posted by: Zaur | March 8th, 2007

    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?

  • Posted by: DM | March 20th, 2007

    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;

  • Posted by: Don | March 20th, 2007

    Zaur:

    The trim() function removes leading and trailing whitespaces of a string.

  • Posted by: Dave | April 5th, 2007

    $outgoing = substr_replace($incoming,”",-1);

  • Posted by: Aaron | April 10th, 2007

    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.

  • Posted by: pRojex | June 20th, 2007

    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.

  • Posted by: Christopher Jason | June 20th, 2007

    pRojex-

    Do you have a URL we could look at? I (or another user) might be able to help.

    - Chris

Post a Comment

You must be logged in to post a comment.