rtrim

Strips whitespace or a given set of characters from the end of a string.

Signature
rtrim(string $string, string $characters = " \n\r\t\v\x00"): string
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$stringstringThe input string.
$charactersoptionalstring= " \n\r\t\v\x00"The set of characters to strip. Ranges may be given with "..", for example "a..z" or "\x00..\x1F".

Return value

stringThe input string with the specified characters removed from its end.

rtrim removes characters from the end of a string as long as each one belongs to the given character set, stopping at the first character that does not. With one argument it strips the six default whitespace bytes: space, tab, newline, carriage return, vertical tab and NUL. chop is an alias of it — a Perl leftover that is best avoided, since PHP's chop behaves nothing like Perl's.

Its two everyday jobs

Cleaning line endings off text read with fgets or from a CRLF file (rtrim($line) handles \r\n and \n alike), and removing a trailing separator you appended once too often — the trailing comma on a generated SQL VALUES list, or the trailing slash on a base URL.

The set trap

Like ltrim, the second argument is a bag of individual characters, not a suffix to remove once. rtrim('This is a short sentence', 'short sentence') returns This is a, because every trailing character happened to be in the bag. And rtrim('1200', '0') returns 12, quietly destroying a number — a real bug when someone uses it to strip trailing decimal zeros without checking whether a decimal point is even present. When you want to remove a known suffix, test with str_ends_with and cut with substr.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Strip the trailing newline from a line read with fgets, whether the file uses LF or CRLF.
  • Remove the final comma and space from a string built in a loop, such as a SQL VALUES list.
  • Normalise a base URL so it never ends in a slash before concatenating a path.

Frequently asked questions

How do I remove only the last character with rtrim?

You cannot — rtrim keeps removing while characters match. Use substr($s, 0, -1) to drop exactly one character, or rtrim only when you genuinely want a run removed.

What is the difference between rtrim and chop?

Nothing: chop is an alias of rtrim. It is named after Perl's chop but behaves completely differently, so prefer rtrim to avoid confusing readers.

Does rtrim remove \r\n line endings?

Yes. Both \r and \n are in the default character set, so rtrim($line) cleans LF and CRLF endings alike without you needing to know which the file used.

More examples

Dropping a trailing separator

The loop always appends a comma and a space, and rtrim removes both in one call because the character set contains each of them. The output ends cleanly at the last closing parenthesis. Note that implode is usually the better tool here — rtrim is the fix when the string was already built incrementally.

Example · php
<?php
$sql = 'INSERT INTO t (a, b, c) VALUES ';
foreach ([1, 2, 3] as $row) {
    $sql .= '(?, ?, ?), ';
}
echo rtrim($sql, ', '), PHP_EOL;

Set semantics, and the safe alternative

The first line collapses to `This is a|` — every trailing character was in the set, so it kept eating. The second reduces `banana` to just `b`. The third shows the correct way to remove a known suffix: str_ends_with confirms it, substr cuts exactly three characters, leaving `ban`.

Example · php
<?php
echo rtrim('This is a short sentence', 'short sentence'), '|', PHP_EOL;
echo rtrim('banana', 'ana'), '|', PHP_EOL;

$s = 'banana';
if (str_ends_with($s, 'ana')) {
    echo substr($s, 0, -3), '|', PHP_EOL;
}

Discussion

  • Be the first to comment on this lesson.