PHP rtrim () function
Examples
Remove the right of the character string:
<?php
$str = "Hello World!";
echo $str . "<br>";
echo rtrim($str,"World!");
?>
$str = "Hello World!";
echo $str . "<br>";
echo rtrim($str,"World!");
?>
Running instance »
Definition and Usage
rtrim () function removes the right of a string of blank characters or other predefined characters.
Related functions:
- LTRIM () - Remove the left side of a string of blank characters or other predefined characters.
- the TRIM () - remove the right of a string of blank characters or other predefined characters.
grammar
rtrim( string,charlist )
参数 | 描述 |
---|---|
string | 必需。规定要检查的字符串。 |
charlist | 可选。规定从字符串中删除哪些字符。如果省略该参数,则移除下列所有字符:
|
technical details
return value: | Returns the modified string. |
---|---|
PHP version: | 4+ |
Update log: | In PHP 4.1, add the charlist parameters. |
More examples
Example 1
Remove string of spaces on the right side:
<?php
$str = "Hello World! ";
echo "Without rtrim: " . $str;
echo "<br>";
echo "With rtrim: " . rtrim($str);
?>
$str = "Hello World! ";
echo "Without rtrim: " . $str;
echo "<br>";
echo "With rtrim: " . rtrim($str);
?>
HTML output of the code above is as follows (view source):
<!DOCTYPE html>
<html>
<body>
Without rtrim: Hello World! <br>With rtrim: Hello World!
</body>
</html>
<html>
<body>
Without rtrim: Hello World! <br>With rtrim: Hello World!
</body>
</html>
Browser output of the code above is as follows:
Without rtrim: Hello World!
With rtrim: Hello World!
With rtrim: Hello World!
Running instance »
Example 2
Remove the string on the right side of the newline (\ n):
<?php
$str = "Hello World!nnn";
echo "Without rtrim: " . $str;
echo "<br>";
echo "With rtrim: " . rtrim($str);
?>
$str = "Hello World!nnn";
echo "Without rtrim: " . $str;
echo "<br>";
echo "With rtrim: " . rtrim($str);
?>
HTML output of the code above is as follows (view source):
<!DOCTYPE html>
<html>
<body>
Without rtrim: Hello World!
<br>With rtrim: Hello World!
</body>
</html>
<html>
<body>
Without rtrim: Hello World!
<br>With rtrim: Hello World!
</body>
</html>
Browser output of the code above is as follows:
Without rtrim: Hello World!
With rtrim: Hello World!
With rtrim: Hello World!
Running instance »
PHP String Reference