PHP: Remove the last character from a string.

In this tutorial, we are going to show you how to remove the last character from a string using PHP.

To do this, we can use either the rtrim function or the substr and mb_substr functions.

Using the rtrim function to remove the last character.

By default, PHP’s rtrim function will strip whitespace from the end of a string.

However, you can also tell it to remove other characters.

Take a look at the following example:

//PHP string
$string = "Hello.";

//Remove the last character using rtrim
$string = rtrim($string, ".");

//Results in "Hello"
var_dump($string);

In the code snippet above, we used rtrim to remove the full stop from the end of our string.

You can also provide PHP’s rtrim function with multiple characters:

//You can provide the rtrim function
//with multiple characters 
$string = rtrim($string, "!o");

If you use the character mask above, you will see that the string “Hello.” becomes “Hell”.

In certain cases, the rtrim function will not work. For example, what if we wanted to remove the last comma in the string below?

//Example string
$string = "test1,test2,test3,,";

//Removing all commas from the 
//end of a string
$string = rtrim($string, ",");

//Results in "test1,test2,test3"
var_dump($string);

The problem here is that rtrim will remove all instances of the character from the end of the string. In other words, it will remove both of the commas even though we only want to remove one.

In cases like this, we will need to use PHP’s substr function instead.

Using PHP’s substr function to remove the last character.

PHP’s substr function will return a part of a given string. However, you can also use it to remove the last character.

Take the following example:

//Example string
$string = "test1,test2,test3,,";

//Remove the last character using substr
$string = substr($string, 0, -1);

//Results in "test1,test2,test3,"
var_dump($string);

In the code above, we provided the substr function with three parameters:

  1. The string that we want to remove our character from.
  2. The $start parameter, which we set to 0, because we want the entire left side of the string.
  3. The $length parameter, which we set to -1. By setting the $length parameter to -1, we are basically telling PHP that we want our sub-string to end before the last character. This leaves us with everything except the final character.

Removing special characters from the end of a PHP string.

In certain cases, you might be dealing with special characters from other languages.

When that happens, the regular substr function will not work.

An example:

//Example string with a special character
$string = "abcdö";

//Attempt to remove the last character
$string = substr($string, 0, -1);

//Results in the replacement question
//mark character or square
var_dump($string);

Here, we attempted to remove the German umlaut character from the end of a string. However, this left us with a garbled string that contains question mark replacement characters.

To remove special characters, you will need to use the mb_substr function instead:

//Use mb_substr for special characters.
$string = mb_substr($string, 0, -1);

If you change substr to mb_substr, you will see that the special character has been successfully removed.

This is because the mb_substr function performs a multi-byte safe operation.

Related: Get the last character of a string using PHP.