The PHP rtrim function can be used to remove spaces from the end of strings, but it can also be used to remove other characters from the end of strings – to remove other characters than the default ones, one would simply list them in the second parameter of the rtrim function.
The default characters to be removed are as follows:
- " " (Space).
- "\t" (Tab).
- "\n" (New line.)
- "\r" (Carriage return.)
- "\0" (NUL-byte.)
- "\x0B" (Vertical tab.)
The rtrim function has just two parameters, the first is the string to be used, the second is the list of characters to be trimmed from the end of the string.
|string||The string to be trimmed.|
|charlist||A list of characters to trim from the end of the string.|
The below script will remove all whitespace from the end of the string, as well as the "abc" characters.
<?php $text = "abcdefg abc abc "; $rtrimmed = rtrim($text, "abc "); echo $rtrimmed; ?>
Note. the "abc " part, removing the blank space would leave the entire $text string intact.