Strings7 min readExamples run on PHP 8.5.10

rtrim()

Learn how PHP 8.4 rtrim() strips whitespace or a custom set of bytes from the end of a string, with tested examples, edge cases and common mistakes.

Signature

rtrim(string $string, string $characters = " \n\r\t\v\x00"): string

Purpose

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.

Example
<?php
$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";
Output
"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.

Example
<?php
$path = "/var/www/html///";
echo rtrim($path, "/"), "\n";

$word = "Hello World";
echo rtrim($word, "Hdle"), "\n";

echo rtrim("graph.php", ".php"), "\n";
Output
/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.

Example
<?php
$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";
Output
"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 $characters is 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 and trim() for both ends.
  • Bytes, not letters: the list is interpreted byte by byte. A multibyte UTF-8 character placed in $characters is 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 plain rtrim().
  • Case sensitivity: rtrim("Hello", "O") does not remove the lowercase o.

Common mistakes

  1. Treating the list as a suffix. rtrim($file, ".php") can eat the end of the filename itself. To remove an exact suffix, check it with str_ends_with() and then cut it with substr().
  2. Ignoring the return value. Calling rtrim($text); on its own statement does nothing useful. Assign the result: $text = rtrim($text);.
  3. Expecting it to clean the middle. Trailing trimming does not collapse repeated spaces within a string.
  4. Trimming numbers by accident. rtrim("100", "0") gives 1. Only strip zeros from strings that are known to have a decimal point, and do so deliberately.
  5. Using it on multibyte text with a custom list. Prefer a Unicode-aware approach, such as a regular expression with the u modifier, when non-ASCII characters are involved.
  • 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

Removing trailing whitespace with the default list
<?php
$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";
Output
"Hello World"
14 11
Custom character list is a set, not a suffix
<?php
$path = "/var/www/html///";
echo rtrim($path, "/"), "\n";

$word = "Hello World";
echo rtrim($word, "Hdle"), "\n";

echo rtrim("graph.php", ".php"), "\n";
Output
/var/www/html
Hello Wor
gra
Using .. ranges for control characters and digits
<?php
$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";
Output
"Example string"
17 14
abc