By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

PHP Rtrim

This Reference shows how the rtrim function of PHP is used to remove characters from the end of strings.

Edited: 2012-12-17 23:34

The PHP rtrim function can be used to remove spaces from the end of strings, but it can also be used to remove other characters from the end of strings – to remove other characters than the default ones, one would simply list them in the second parameter of the rtrim function.

See also: Remove spaces from end of string with PHP

The default characters to be removed are as follows:

  1. " " (Space).
  2. "\t" (Tab).
  3. "\n" (New line.)
  4. "\r" (Carriage return.)
  5. "\0" (NUL-byte.)
  6. "\x0B" (Vertical tab.)


The rtrim function has just two parameters, the first is the string to be used, the second is the list of characters to be trimmed from the end of the string.

stringThe string to be trimmed.
charlistA list of characters to trim from the end of the string.

Rtrim Example

The below script will remove all whitespace from the end of the string, as well as the "abc" characters.

 $text = "abcdefg abc abc        ";
 $rtrimmed = rtrim($text, "abc ");
 echo $rtrimmed;

Note. the "abc " part, removing the blank space would leave the entire $text string intact.