To remove certain characters from the end of a string using PHP, you would typically use the rtrim function, which will remove characters from the right end of the string.
Without the second parameter the rtrim function will only remove whitespace characters such as:
- " " (Space).
- "\t" (Tab).
- "\n" (New line.)
- "\r" (Carriage return.)
- "\0" (NUL-byte.)
- "\x0B" (Vertical tab.)
The below script will only remove whitespace from the end of a string, it is using the rtrim function.
<?php $text = "\t\tJust an example string :-) \t"; $rtrimmed = rtrim($text); echo $rtrimmed; ?>
Removing other characters
To remove other characters than the default ones, you will have to use the second parameter of the rtrim function. An example of how to remove the "abc" letters from the end of the string is included below:
<?php $text = "abcdefg abc abc"; $rtrimmed = rtrim($text, "abc"); echo $rtrimmed; ?>
Note. The string will be re-checked, meaning that both "abc" parts will be removed from the end.