Signature
rtrim(string $string, string $characters = " \n\r\t\v\x00"): stringPurpose
rtrim() removes unwanted characters from the end of a string and returns the result. By default it removes whitespace-like bytes, which makes it useful for cleaning lines of text, removing trailing newlines, and tidying values before they are compared or stored. With the second argument you can supply your own list of characters, such as a trailing slash or trailing zeros.
The function never changes the variable you pass in. It returns a new string, so you must use the return value.
Signature
Parameters and defaults
$string: the input string.$characters: optional. Every character in this string is a candidate for removal. The order of the characters does not matter, and a character may be listed more than once.
When $characters is omitted, rtrim() strips these bytes: an ordinary space, a tab (\t), a line feed (\n), a carriage return (\r), a vertical tab (\v) and the NUL byte (\x00).
Inside $characters you can write a range with .., for example "0..9" for all ASCII digits or "\x00..\x1F" for all ASCII control characters. Write ranges in ascending order, from the lower byte to the higher one.
Return value and failure cases
rtrim() always returns a string. If nothing at the end matches, you get the original content back unchanged. If every character matches, you get an empty string. There is no false return value for ordinary string input. If your file uses declare(strict_types=1), passing a value that is not a string is a type error, so convert or validate input first.
Example 1: removing trailing whitespace
The default character list handles spaces, tabs and line breaks. The example uses json_encode() and strlen() so the trimmed result is visible.
$line = "Hello World \t\n";
$clean = rtrim($line);
echo json_encode($clean, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
echo strlen($line), " ", strlen($clean), "\n";"Hello World" 14 11
The original string has 14 bytes and the cleaned one has 11. Only the end is affected; whitespace inside the text stays.
Example 2: a custom character list
The second argument is a set of characters, not a suffix. That distinction matters, as the third line shows.
$path = "/var/www/html///";
echo rtrim($path, "/"), "\n";
$word = "Hello World";
echo rtrim($word, "Hdle"), "\n";
echo rtrim("graph.php", ".php"), "\n";/var/www/html Hello Wor gra
The first call removes all trailing slashes. The second removes d and l, then stops at r because r is not in the list. The third call does not remove just .php. The letters p, h and the dot are each in the set, so trimming continues past the extension until it reaches a, leaving gra.
Example 3: ranges
Use .. to describe a run of bytes without typing each one. This is handy for stripping control characters or digits.
$binary = "Example string\x09\x0A\x01";
$clean = rtrim($binary, "\x00..\x1F");
echo json_encode($clean), "\n";
echo strlen($binary), " ", strlen($clean), "\n";
$digits = rtrim("abc123987", "0..9");
echo $digits, "\n";"Example string" 17 14 abc
The control-character range covers the tab, line feed and \x01 at the end. The digit range removes every trailing digit and stops at c.
Edge cases
- Empty character list: if
$charactersis an empty string, nothing is removed. - Empty input: an empty string returns an empty string.
- Only the end is touched: matching characters at the start or in the middle are left alone. Use
ltrim()for the start andtrim()for both ends. - Bytes, not letters: the list is interpreted byte by byte. A multibyte UTF-8 character placed in
$charactersis treated as several separate bytes, which can remove part of another character. A non-breaking space is also not in the default list, so it survives a plainrtrim(). - Case sensitivity:
rtrim("Hello", "O")does not remove the lowercaseo.
Common mistakes
- Treating the list as a suffix.
rtrim($file, ".php")can eat the end of the filename itself. To remove an exact suffix, check it withstr_ends_with()and then cut it withsubstr(). - Ignoring the return value. Calling
rtrim($text);on its own statement does nothing useful. Assign the result:$text = rtrim($text);. - Expecting it to clean the middle. Trailing trimming does not collapse repeated spaces within a string.
- Trimming numbers by accident.
rtrim("100", "0")gives1. Only strip zeros from strings that are known to have a decimal point, and do so deliberately. - Using it on multibyte text with a custom list. Prefer a Unicode-aware approach, such as a regular expression with the
umodifier, when non-ASCII characters are involved.
Related functions
trim()strips from both the beginning and the end.ltrim()strips from the beginning only.str_ends_with()tests for an exact suffix.substr()extracts part of a string, for example to remove a known suffix.
Recap
rtrim() returns a copy of a string with trailing characters removed. By default it removes spaces, tabs, line breaks, vertical tabs and NUL bytes. A custom second argument is a set of bytes, may include .. ranges, and is not a literal suffix. Always use the returned value, and be careful with multibyte text.
Tested examples
$line = "Hello World \t\n";
$clean = rtrim($line);
echo json_encode($clean, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
echo strlen($line), " ", strlen($clean), "\n";
"Hello World" 14 11
$path = "/var/www/html///";
echo rtrim($path, "/"), "\n";
$word = "Hello World";
echo rtrim($word, "Hdle"), "\n";
echo rtrim("graph.php", ".php"), "\n";
/var/www/html Hello Wor gra
$binary = "Example string\x09\x0A\x01";
$clean = rtrim($binary, "\x00..\x1F");
echo json_encode($clean), "\n";
echo strlen($binary), " ", strlen($clean), "\n";
$digits = rtrim("abc123987", "0..9");
echo $digits, "\n";
"Example string" 17 14 abc