rtrim
Delete whitespace characters (or other characters) at the end of the string
rtrim()
function removes whitespace characters or other predefined characters to the right of the string.
ltrim()
- Removes whitespace or other predefined characters to the left of the stringtrim()
- Remove whitespace or other predefined characters on both sides of the stringRemove characters from the right side of the string:
<?php $str = "Hello World!" ; echo $str . "<br>" ; echo rtrim ( $str , "World!" ) ; ?>
Try it yourself
Remove the space to the right of the string:
<?php $str = "Hello World!" ; echo "Don't use rtrim:" . $str ; echo "<br>" ; echo "Use rtrim:" . rtrim ( $str ) ; ?>
The HTML output of the above code is as follows (see the source code):
< ! DOCTYPE html > < html > < body > Don't use rtrim: Hello World ! < br > Use rtrim: Hello World ! < / body > < / html >
The browser output of the above code:
Don't use rtrim: Hello World! Use rtrim: Hello World!
Try it yourself
Remove the newline character (\n) to the right of the string:
<?php $str = "Hello World!\n\n\n" ; echo "Don't use rtrim:" . $str ; echo "<br>" ; echo "Use rtrim:" . rtrim ( $str ) ; ?>
The HTML output of the above code is as follows (see the source code):
< ! DOCTYPE html > < html > < body > Don't use rtrim: Hello World ! < br > Use rtrim: Hello World ! < / body > < / html >
The browser output of the above code:
Don't use rtrim: Hello World! Use rtrim: Hello World!
Try it yourself
rtrim ( string , charlist )
parameter | describe |
---|---|
string | Required. Specifies the string to be checked. |
charlist |
Optional. Specifies which characters to delete from the string. If omitted, remove all the following characters:
|