How to remove whitespace from the end of a string in PHP

The rtrim() function is a built-in function in PHP which is used to removes whitespace from the end of a string.



string – This parameter is required. It specifies the string to be checked.

charlist – This parameter is optional. It specifies which characters are to be removed from the string. These functions will remove the following whitespace characters:

  • Space character (” “)
  • Tab character (“t”)
  • Vertical tab character (“v”)
  • Newline character (“n”)
  • Carriage return character (“r”)
  • Null-byte character (“x0B”)


$str = 'PHP Tutorials!!! ';
echo strlen($str); // Output: 21
echo '<br>';
$trim_str = rtrim($str);
echo strlen($trim_str); // Output: 16

Praveen Kumar

I am a fullstack web developer with a passion for SEO, creating stunning websites, and organic marketing. I am a perfectionist.

Post navigation