Backend Development 3 min read

PHP rtrim() Function: Description, Parameters, Return Value, and Usage Examples

This article explains PHP's rtrim() function, detailing its syntax, parameters, default characters removed, and provides multiple code examples demonstrating how to trim whitespace, custom character masks, and ASCII control characters from the end of strings.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP rtrim() Function: Description, Parameters, Return Value, and Usage Examples

The article introduces PHP's rtrim() function, which removes whitespace or specified characters from the end of a string.

Syntax: string rtrim(string $str [, string $character_mask])

Parameters: • $str : The input string. • $character_mask (optional): A list of characters to strip; ranges can be specified with .. .

Return value: The function returns the trimmed string. Without a mask, it removes the following characters: space (0x20), tab (0x09), newline (0x0A), carriage return (0x0D), NUL (0x00), and vertical tab (0x0B).

Example usage:

Output of the example:

string(32) "    These are a few words :) ... "
string(16) "    Example string\n"
string(11) "Hello World"
string(30) "    These are a few words :) ..."
string(26) "    These are a few words :)"
string(9) "Hello Wor"
string(15) "    Example string"

An additional example shows how to strip all ASCII control characters from a binary string using a mask like "\x00..\x1F" , demonstrating the flexibility of rtrim() for custom trimming needs.

BackendPHPstring-manipulationrtrimcharacter masktrim functions
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.