PHP rtrim()Functions

PHP String 参考手册PHP String Reference Manual

Examples

Remove characters from the right side of a string:

<?php
$str = "Hello World!";
echo $str . "<br>";
echo rtrim($str,"World!");
?>

Run Example »

Definition and Usage

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

Related functions:

  • ltrim()- Removes whitespace or other predefined characters from the left side of a string.
  • trim() - Removes whitespace or other predefined characters from both sides of a string.

Syntax

rtrim(string,charlist)

Parameter Description
string Required. Specifies the string to check.
charlist Optional. Specifies which characters to remove from the string. If this parameter is omitted, all of the following characters are removed:
  • "\0" - NULL
  • "\t" - Tab
  • "\n" - Newline
  • "\x0B" - Vertical tab
  • "\r" - Carriage return
  • " " - Space

Technical Details

Return value: Returns the modified string.
PHP Version: 4+
Changelog: In PHP 4.1, thecharlistparameter was added.


More Examples

Example 1

Remove spaces from the right side of a string:

<?php
$str = "Hello World! ";
echo "Without rtrim: " . $str;
echo "<br>";
echo "With rtrim: " . rtrim($str);
?>

The HTML output of the above code is as follows (view source):

<!DOCTYPE html>
<html>
<body>

Without rtrim: Hello World! <br>With rtrim: Hello World!
</body>
</html>

The browser output of the above code is as follows:

Without rtrim: Hello World!
With rtrim: Hello World!

Run Example »

Example 2

Remove newline characters (\n) from the right side of a string:

<?php
$str = "Hello World!nnn";
echo "Without rtrim: " . $str;
echo "<br>";
echo "With rtrim: " . rtrim($str);
?>

The HTML output of the above code is as follows (view source):

<!DOCTYPE html>
<html>
<body>

Without rtrim: Hello World!


<br>With rtrim: Hello World!
</body>
</html>

The browser output of the above code is as follows:

Without rtrim: Hello World!
With rtrim: Hello World!

Run Example »


PHP String 参考手册PHP String Reference Manual Other Extensions