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
<?php
$text = "\t\tThese are a few words :) ... ";
$binary = "\x09Example string\x0A";
$hello = "Hello World";
var_dump($text, $binary, $hello);
$trimmed = rtrim($text);
var_dump($trimmed);
$trimmed = rtrim($text, " \t.");
var_dump($trimmed);
$clean = rtrim($binary, "\x00..\x1F");
var_dump($clean);
?>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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
