rtrim()
Our article is about the PHP function rtrim(), which is used to remove whitespace (or other characters) from the end of a string. This function is useful for
The PHP rtrim() function removes whitespace — or any other characters you specify — from the right end of a string. ("r" stands for right.) It returns a new, trimmed string and leaves the original untouched, since strings are passed by value in PHP.
This page covers the syntax, which characters are stripped by default, how to supply a custom character mask (including ranges), and when rtrim() is the right tool versus its siblings ltrim() and trim().
Syntax
rtrim(string $string, string $characters = " \n\r\t\v\x00"): string| Parameter | Required | Description |
|---|---|---|
$string | Yes | The input string to trim. |
$characters | No | A list of characters to strip from the end. If omitted, the default whitespace set is used. |
Return value: the trimmed string. The function never modifies $string in place.
Characters stripped by default
When you call rtrim() with only one argument, it removes these trailing characters:
| Character | Meaning |
|---|---|
" " (0x20) | Ordinary space |
"\t" (0x09) | Tab |
"\n" (0x0A) | Line feed (newline) |
"\r" (0x0D) | Carriage return |
"\0" (0x00) | NUL byte |
"\v" (0x0B) | Vertical tab |
Note that this default set does not include other Unicode whitespace such as a non-breaking space (\xA0); to strip those you must list them explicitly in the mask.
Removing trailing whitespace
The most common use is cleaning up trailing spaces, tabs, and line breaks — for example after reading a line from a file or sanitizing form input.
The | marks where the string now ends, so you can see the trailing spaces are gone:
Hello World!|Newlines and tabs are handled the same way:
<?php
$line = "value\t\r\n";
echo rtrim($line) . '|'; // value|
?>Trimming specific characters with a mask
Pass a second argument to strip a custom set of characters instead of whitespace. Every character listed in the mask is removed from the end, repeatedly, until a character not in the mask is reached.
<?php
echo rtrim('Hello World...', '.') . "\n"; // Hello World
echo rtrim('/path/to/dir///', '/') . "\n"; // /path/to/dir
echo rtrim('cleanup.txt.bak', '.bak') . "\n"; // cleanup.txt — strips any of '.', 'b', 'a', 'k'
?>The third line is a common gotcha: the mask is a set of single characters, not a substring. '.bak' means "remove any of the characters ., b, a, k", so it keeps eating until it hits the t in txt. If you need to remove a literal suffix string, use str_replace() or substr() instead.
Character ranges
You can express a contiguous range with .. between two characters — handy for stripping all trailing digits or letters:
<?php
echo rtrim('item42', '0..9') . "\n"; // item — strips trailing digits 0-9
?>rtrim() vs ltrim() vs trim()
These three functions share the same signature and character-mask rules; they differ only in which side of the string they affect:
| Function | Trims from |
|---|---|
ltrim() | Left (start) only |
rtrim() | Right (end) only |
trim() | Both ends |
Reach for rtrim() specifically when leading whitespace is meaningful but trailing whitespace is noise — for instance, preserving indentation while dropping line-ending artifacts.
Summary
rtrim()strips whitespace (or a custom character set) from the end of a string and returns the result.- With no second argument it removes space, tab, newline, carriage return, NUL, and vertical tab.
- The optional mask is a set of characters, not a suffix — to remove a literal ending, use
str_replace(). - Use
ltrim()for the start andtrim()for both ends.