mb_rtrim

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

Signature
mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string
  • Added in PHP 8.4.0

Parameters

ParameterTypeDescription
$stringstringThe input string.
$charactersoptional?string= nullThe set of characters to strip, listed one after another. If omitted or null, a default set of Unicode whitespace characters is used.
$encodingoptional?string= nullThe character encoding of the string. If omitted or null, the internal encoding is used.

Return value

stringThe string with the matching characters removed from its end.

Errors

Throws a ValueError if encoding is not a name mbstring recognises.

mb_rtrim() removes characters from the end of a string, counting in characters rather than bytes. It is the mirror of mb_ltrim() and shares its two advantages over the byte-based rtrim().

The default whitespace set is the Unicode one, not the six ASCII characters rtrim() knows. That matters because trailing whitespace is where the exotic characters actually collect: a no-break space U+00A0 at the end of a pasted cell, an ideographic space U+3000 after Japanese text, a narrow no-break space before a currency symbol. rtrim() walks backwards from the last byte, finds a byte such as 0xA0 that is not in its set, and stops immediately — leaving the invisible character in place and, worse, leaving you with two strings that look identical but compare unequal.

The custom characters argument is likewise matched as whole characters, so trimming an ellipsis or a typographic quote cannot leave half a character behind and produce invalid bytes.

It is a set, not a suffix: every trailing character present in the set is removed, repeatedly, until one is not. Trimming "0" from "1200" gives "12", not "120". And as always, only the end of the string is touched — use mb_trim() for both ends.

Example

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

When to use it

  • Normalise pasted spreadsheet cells that end in a no-break space before comparing them.
  • Strip a trailing ideographic space from Japanese form input.
  • Remove trailing zeros and a stray decimal point from a formatted price.
  • Drop trailing slashes from a path or URL fragment before storing it.

Frequently asked questions

What is the difference between rtrim and mb_rtrim?

rtrim() works byte by byte and only knows six ASCII whitespace characters, so it cannot remove a trailing no-break or ideographic space. mb_rtrim() works on characters and uses the full Unicode whitespace set.

How do I remove a trailing non-breaking space in PHP?

Call mb_rtrim($string) — U+00A0 is in its default whitespace set. On PHP 8.3 and earlier, use preg_replace('/[\s\x{00A0}]+$/u', '', $string) instead.

Does mb_rtrim remove a suffix from a string?

No. The second argument is a set of individual characters, each of which is stripped repeatedly from the end. To remove a whole suffix once, use str_ends_with() with mb_substr(), or preg_replace() with an anchored pattern.

More examples

Why two identical-looking strings compare unequal

The first two comparisons are false: trim() cannot see U+00A0, so it changes nothing. mb_rtrim() removes it and the comparison finally succeeds. This is the everyday failure behind "but the values look the same in the database" bug reports.

Example · php
<?php
$a = "Total\u{00A0}";   // trailing no-break space
$b = "Total";

var_dump($a === $b);
var_dump(trim($a) === $b);
var_dump(mb_rtrim($a) === $b);
echo strlen($a), " vs ", strlen(mb_rtrim($a)), " bytes\n";

Tidy up formatted numbers

The inner call strips every trailing zero and the outer one removes the decimal point left stranded on "3.000". The last row shows why the order matters: "40" becomes "4" because the trailing zero is significant here, so guard number formatting with a check for a decimal point before applying this pattern.

Example · php
<?php
$prices = ['12.500', '3.000', '0.010', '40'];

foreach ($prices as $p) {
    echo str_pad($p, 8), '-> ', mb_rtrim(mb_rtrim($p, '0'), '.'), "\n";
}

A set of characters, not a suffix

Each trailing character that appears anywhere in the set is removed, over and over, which is why "HEADabcabc" loses six characters rather than three. The last line removes all three repetitions of 語 as whole characters, something rtrim() could only do by luck when the byte sets happen not to overlap.

Example · php
<?php
echo mb_rtrim("report.txt...", "."), "\n";
echo mb_rtrim("value»«»", "«»"), "\n";
echo mb_rtrim("HEADabcabc", "abc"), "\n";
echo mb_rtrim("日本語語語", "語"), "\n";

Discussion

  • Be the first to comment on this lesson.