PHP rtrim() Function: Removing Trailing Characters
This article explains the PHP rtrim() function, describing how it removes trailing whitespace or specified characters from a string, detailing its parameters and return value, and providing multiple code examples that demonstrate typical usage and edge‑case handling.
rtrim() removes trailing whitespace characters (or other specified characters) from a string and returns the modified string.
The function accepts two parameters: string $str , the input string, and an optional string $character_mask that lists characters to be stripped; ranges can be defined with .. .
If $character_mask is omitted, rtrim() removes the following characters by default: space (ASCII 32), tab (\t), newline (\n), carriage return (\r), NUL byte (\0), and vertical tab (\x0B).
Parameters
str : The input string.
character_mask : Optional list or range of characters to remove.
Return value
Returns the string after the specified characters have been stripped from its end.
Example usage
The first var_dump shows the original variables, the second displays the result of rtrim($text) (removing default whitespace), the third shows removal of spaces, tabs, and periods, and the final dump demonstrates stripping control characters from a binary string.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.