HOWTO · PHP
How to Remove the Last Character From a String in PHP
This article introduces how to remove the last character from a string in PHP. It includes the rtrim() function and the substr() function. Discover the best methods for string manipulation in PHP and enhance your programming skills with practical examples and clear explanations.
On this page
To remove content from the end of a PHP string
To remove content from the end of a PHP string, choose the function based on what should be removed. Use rtrim() to strip any number of trailing bytes that belong to a specified character set. Use substr() with a length of -1 to keep the string except for its final byte. PHP strings are byte-oriented, so use a multibyte-aware function when “character” means a complete Unicode character.
Use rtrim() to remove a trailing character set
rtrim() removes characters from the right side of a string while they belong to its second argument, called a character mask. The mask is a set of eligible characters, not a suffix that must match as one complete sequence. The function keeps removing bytes from that set until it reaches a byte that is not in the mask. As a result, it can remove several different trailing characters and repeated copies of one character.
For a value that may end with either of two punctuation marks, this complete example applies the mask !?:
<?php
$value = "report!?!!";
$clean = rtrim($value, "!?");
echo $clean, PHP_EOL;
report
The trailing exclamation marks and question mark all belong to the mask, so each is removed from the end until the preceding t is reached.
Use this method when the rule is “remove trailing punctuation or padding from this set.” Specify only characters that should be removable. For example, the mask !?. permits any sequence of those three characters at the end to be removed; it does not mean “remove the literal suffix !?..” If the string ends with a character outside the mask, that character and everything before it remain. An empty string remains empty.
Use substr() to remove the final character
For an ASCII string where the goal is to remove exactly its last character, substr($string, 0, -1) keeps the prefix and omits the final byte. The start offset 0 begins at the start of the string. A negative length counts back from the end, so -1 makes the returned portion stop one byte before the end.
<?php
$string = "Hello World!";
$withoutLastByte = substr($string, 0, -1);
echo $withoutLastByte, PHP_EOL;
Hello World
This code omits the final byte regardless of whether it is a letter, punctuation mark, or space; unlike rtrim(), it does not check the byte’s value.
PHP strings and substr() are byte-oriented. In UTF-8, one displayed character can use several bytes, so removing one byte from a string ending in a multibyte character can leave an incomplete byte sequence rather than remove one complete character. To remove one Unicode character, use mb_substr($string, 0, -1, 'UTF-8') with the mbstring extension enabled. Choose the encoding that matches the data when it is not UTF-8.
Choose the right approach and handle edge cases
Use rtrim() when the removal rule names a set of trailing characters and may remove several of them. Use substr() when the rule is to discard a fixed number of bytes regardless of their values. For a plain ASCII value and one final character, substr($string, 0, -1) expresses that intent directly. For padding made from several possible characters, give rtrim() the exact mask you want.
Both functions return a string; neither changes the original variable in place. Assign the result if later code needs the cleaned value. Before choosing a method, consider whether the input can be empty, whether the target character is actually present at the end, and whether the data uses multibyte encoding. The empty string remains empty with either operation, while rtrim() leaves a nonmatching ending alone. substr() still removes one byte without checking its meaning.
A practical rule is to use rtrim() for a trailing character set and substr() for a fixed byte count. If “character” means a complete Unicode code point in your application, use the corresponding multibyte string function and ensure its extension and encoding are configured. These distinctions also help when adapting the examples: change the mask to define which trailing bytes may be removed, or change the substr() length to define how many bytes should be omitted.