PHP rtrim() Function

Quick summary: The PHP rtrim() function removes whitespace or specified characters from the end of a string.

PHP rtrim() Syntax

rtrim(string $string, string $characters = " \n\r\t\v\x00"): string
PHP

PHP rtrim() Basic examples

$text = "Hello world   ";
echo rtrim($text);
PHP
Output:
Hello world

Removes trailing whitespace from a string.

$text = "***Hello***";
echo rtrim($text, "*");
PHP
Output:
***Hello

Removes specified characters only from the end.

$text = "Hello\n\t";
echo rtrim($text);
PHP
Output:
Hello

Removes newlines and tabs from the end of the string.

PHP rtrim() Real-world usage

$url = "https://example.com/";
echo rtrim($url, "/");
PHP
Output:
https://example.com

Removes a trailing slash from a URL.

$logLine = "ERROR: failed\n";
echo rtrim($logLine);
PHP
Output:
ERROR: failed

Cleans newline characters from log entries.

PHP rtrim() Edge cases

$text = "";
echo rtrim($text);
PHP

Calling rtrim() on an empty string returns an empty string.

$text = "   ";
echo rtrim($text);
PHP

A string containing only whitespace becomes empty.

PHP rtrim() Common mistakes

Using rtrim when ltrim is needed

rtrim() does not affect the beginning of a string.

Incorrect
echo rtrim("***Hello", "*");
Correct
echo ltrim("***Hello", "*");

Use ltrim() to remove characters from the start.

Removing too many characters with a mask

Character masks remove all listed characters, not sequences.

Incorrect
echo rtrim("Hello000", "0");
Correct
echo preg_replace('/0+$/', '', "Hello000");

Use a regex when precise control is required.

PHP rtrim() Frequently Asked Questions

What does rtrim() do in PHP?

rtrim() removes whitespace or specified characters from the end of a string.

What characters does rtrim() remove by default?

By default, rtrim() removes spaces, tabs, newlines, carriage returns, vertical tabs, and null bytes from the end.

What is the difference between rtrim() and trim()?

rtrim() removes characters from the end only, while trim() removes from both the beginning and end.

What is the difference between rtrim() and ltrim()?

rtrim() removes characters from the end, while ltrim() removes from the beginning of a string.

How to remove specific characters using rtrim()?

Pass a character mask as the second argument, for example rtrim($string, "*") to remove asterisks from the end.

Does rtrim() remove spaces in the middle of a string?

No, rtrim() only removes characters from the end and does not affect the middle of the string.

Does rtrim() modify the original string?

No, rtrim() returns a new string and does not modify the original variable unless reassigned.

Can rtrim() remove numbers or letters?

Yes, if specified in the character mask, rtrim() can remove any characters from the end.

What happens if the string is empty?

rtrim() returns an empty string when applied to an empty string.

Does rtrim() work with multibyte strings?

No, rtrim() is not multibyte-safe. Use regex or mb functions for Unicode strings.

Is rtrim() useful for file paths?

Yes, rtrim() is often used to remove trailing slashes or characters from file paths.

What is a real-world use case of rtrim()?

rtrim() is commonly used to clean trailing whitespace, remove delimiters, or normalize input data.

PHP rtrim() Related PHP Functions