PHP rtrim()
Published May 1, 2023Updated May 15, 2024
Contribute to Docs
The rtrim() function removes whitespace or other predefined characters from the right side of a string.
Syntax
rtrim($string, $charlist)
The
$stringparameter is the string from which characters are removed.The
$charlistparameter 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 linex0B- vertical tab\r- carriage return" "- space
Example
The following example removes specified characters from the right side of a string:
<?php$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:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PHP on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours