Published May 1, 2023
Contribute to Docs

The rtrim() function removes whitespace or other predefined characters from the right side of a string.


rtrim($string, $charlist)
  • The $string parameter is the string from which characters are removed.

  • The $charlist parameter specifies which characters to remove from the string. If omitted, then the following characters are removed from the right side (if applicable):

  • \0 - NULL

  • \t - tab

  • \n - new line

  • x0B - vertical tab

  • \r - carriage return

  • " " - space


The following example removes specified characters from the right side of a string:

$str = "Hello World!\n";
echo "Without rtrim: " . $str;
echo "With rtrim: " . rtrim($str, "World!\n");

This results in the following output:

Without rtrim: Hello World!
With rtrim: Hello

Codebyte Example

The following runnable example removes whitespaces from the right side of the string:


All contributors

Looking to contribute?

Learn PHP on Codecademy