Use PHP to strip the last character in a string
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:
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:
$newstring = substr($string, 0, -2);
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 can also use rtrim().
The second, optional parameter of rtrim() is a character list. To use your example:
Printing or echo-ing $newstring would give you the same result.
You can also avoid the problem entirely by using implode(), so you don’t have the superfluous delimiters in the first place. :-)
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?
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;
The trim() function removes leading and trailing whitespaces of a string.
$outgoing = substr_replace($incoming,”",-1);
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.
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.
Do you have a URL we could look at? I (or another user) might be able to help.
Post a Comment
You must be logged in to post a comment.