John Mwaniki /   19 May 2023

How to remove the last character from a string in PHP

When working with PHP, it's common to do string manipulations in some scenarios. Among these many manipulations is removing the last character from a string.

Whether you want to get rid of a trailing whitespace, a punctuation mark, or any other character at the end of a string, knowing how to accomplish this task is essential.

In this article, you will learn different methods to remove the last character from a string in PHP with an example of each method.

Method 1: Using substr() function

The substr() function extracts and returns a portion of a string.

Syntax

substr(string,start,length)

Parameters Description

  • string: Specifies the string which we want to get a part of.
  • start: Specifies the position in the string where to start from (counting from zero). If set to a negative value, the string starts at the specified position from the end of the string. If the specified start value is bigger than the length of the string, then an empty string will be returned.
  • length: This is an optional parameter that specifies the length of the string to be returned. If not specified, it defaults to up to the end of the string. If set to a negative value, then the number of characters will be omitted from the end of the string. If set to 0, an empty string will be returned.

By specifying the start parameter as 0 and the length parameter as -1, we can remove the last character of the string.

Example

<?php
$string = "Hello World!";
$newstring = substr($string, 0, -1);
echo $newstring;
?>

Output:

Hello World

In the example above, the substr() function excludes the last character and returns a substring starting from the first character (index 0) up to the second last character.

If you wish to remove the last two or three characters from the string instead of one, you can substitute "-1" above with "-2" or "-3" respectively, and so on.

Method 2: Using mb_substr() function

In English, each character takes one byte. However, UTF-8 encoded text such as accents (eg ä, é, に, ち) or even emojis take more than one byte (referred to as multi-byte characters).

If you are working with multibyte strings, the substr() function definitely won't handle them as expected since it assumes one byte per character.

The mb_substr() function is the best option in this case as it is the multibyte-safe version of substr().

Example

<?php
$string = "こんにちは";
$newstring = mb_substr($string, 0, -1);
echo $newstring;
?>

Output:

こんにち

In this example, mb_substr() is used to extract the substring from the first character (index 0) up to the second last character, taking into account the multibyte nature of the string.

Method 3: Using rtrim() function

The rtrim() function is primarily used to remove trailing whitespace from a string. However, it is often used to remove other predefined characters from the right side of a string.

Syntax

rtrim(string,characters)

Parameters Description

  • string: Specifies the input string.
  • characters: This is an optional parameter that specifies the character(s) that you would want to strip off from the end of the string.

The rtrim() removes any number of characters, listed in the second parameter, from the end of the string and returns the modified string.

Example

<?php
$string = "a, b, c, d, e, f,";
$newstring = rtrim($string, ",");
echo $newstring;
?>

Output:

a, b, c, d, e, f

In this example, rtrim() is applied with the second parameter being the last character of the string. By specifying the character to remove, rtrim() trims all occurrences of that character from the right end of the string, effectively eliminating the last character.

The downside of this method is when the string has more than one occurrence of what is specified in the second parameter, it ends up eliminating all these occurrences.

Example

<?php
$string = "Hello World!!";
$newstring = rtrim($string, "!");
echo $newstring;
?>

Output:

Hello World

In the above example, the string had two exclamation marks !! at the end. I just wanted to remove one and retain one but the function ended up removing both. In such a case, rtrim() won't be the best option.

Method 4: Using substr_replace() function

The substr_replace() function replaces part of a string with another string and returns the resulting string.

Syntax

substr_replace(string,replacement,start,length)

Parameters Description

  • string: Specifies the string in which to modify.
  • replacement: Specifies the string to replace a part of the original string with.
  • start: Specifies where to start replacing the string (counting from 0). If set to a negative value, the replacement starts at the specified position from the end of the string.
  • length: This is an optional parameter that specifies how many characters should be replaced. if set to a negative value it specifies the number of characters from the end of the string at which to stop replacing. When not set, it defaults to the same length as the string, replacing up to the end of the string. If set to zero, this inserts the replacement string into the string at the given start position.

By specifying the start parameter as -1 and the replacement as an empty string, we can remove the last character of the string.

Example

<?php
$string = "Hello World!";
$newstring = substr_replace($string, "", -1);
echo $newstring;
?>

Output:

Hello World

Conclusion

Removing the last character from a string in PHP is necessary for various scenarios. In this article, we have covered four methods to accomplish that which include: substr(), mb_substr(), rtrim(), and substr_replace().

Depending on your specific requirements and the type of strings you are working with, choose the appropriate method and incorporate it into your PHP code.