As with many things in PHP, manipulating strings can be handled a number of ways. I often find myself creating a string of items from an array for presentation on screen or entry into a database. I figured now would be a good time to explore the best method of removing that pesky comma that usually ends up at the end of a looped string (which just so happens to be the wrong way of going about it).

In this article, I will show you four different ways of removing the last character of a string and then the best way to do it, which doesn’t involve removing any character but it still likely the reason you are here.

Are you trying to get rid of a trailing comma?

Most of the time, developers want to loop through items to create a comma-separated list as a string.

The quick and dirty method of this is to simply loop through items and add a comma to the end of each. After the loop, you would then need to remove the last comma from the end of your string.

If you are ending up with a comma in this manner, you should be taking advantage of PHP’s implode method instead of looping through and concatenating a comma at each step.

Here is a clean way to create your comma separated list – no string manipulation needed.

$foods = array("banana","orange","apple");
 
$stringarray = array();
foreach($foods as $fruit)
{
	array_push($stringarray,$fruit); // step #1 - add values to an array
}
$string = implode("','",$stringarray); // step #2 - implode array
 
echo "SELECT * FROM menu WHERE fruits IN ('$string')";

The output from the above example will be
SELECT * FROM menu WHERE fruits IN ('banana','orange','apple').

Removing the last character from a string

rtrim is a PHP function that can also be used to strip off the last character. For the trailing comma situation, you can use rtrim($string, ",").

If you are in a real mess and end up with commas at the beginning AND end of your strings, you can use the regular trim instead of rtrim. trim($string, ",") will take commas off of both ends of the string. Of course, you can use trim and rtrim for any characters.

Both of those functions require that you specify the character(s) to get rid of. Sometimes you don’t know what the last character is and simply want to drop it anyway.

In this case, you want to use substr($string, 0, -1) or substr_replace($string, "", -1). These two methods will both remove characters from the end of a string, regardless of what those characters are. You can also change the -1 to a larger number and remove more characters.

So to review our options:

/* Method 1 */ rtrim("Some random text.", ".")
/* Method 2 */ trim("Some random text.", ".")
/* Method 3 */ substr("Some random text.", 0, -1)
/* Method 4 */ substr_replace("Some random text.", "", -1)

Since this is SolidlyStated, I am not going to leave you with 4 options. I am going to leave you with 1.

Choose substr. It’s the clear winner. I can’t remember the last time I had to legitimately use any of the other 3 methods and the first 2 aren’t as flexible to begin with (since you need to know the characters you are removing).

substr is versatile and gets used all the time for other string work. Simply go with that from now on. Solidly Stated.

PHP performance substr vs rtrim

For those of you who were wondering (and I know you are out there), substr even performs better than rtrim.

Here are the results of doing one million iterations on the following two lines:

rtrim("this random string of text is short,",","); // takes 1.46 seconds
substr("this random string of text is short,",0,-1); // takes 1.42 seconds

Substr is faster for large amounts of text as well.