Backend Development 3 min read

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.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP rtrim() Function: Removing Trailing Characters

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.

BackendTutorialrtrimstring-manipulation
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.