mb_trim
Strips whitespace, including Unicode spaces, from both ends of a string.
mb_trim(string $string, ?string $characters = null, ?string $encoding = null): string- Added in PHP 8.4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$string | string | The string that will be trimmed. |
$charactersoptional | ?string= null | The set of characters to strip, listed one after another. Null means the default Unicode whitespace set. |
$encodingoptional | ?string= null | Character encoding of the string. Falls back to the internal encoding when null. |
Return value
stringThe string with the specified characters removed from both ends.
Errors
Throws a ValueError if encoding names an unknown character encoding.
Added in PHP 8.4, mb_trim() is the Unicode-aware counterpart to trim(). Where trim() only removes six ASCII bytes — space, tab, newline, carriage return, NUL and vertical tab — mb_trim() also removes the whole Unicode whitespace family: non-breaking space (U+00A0), the en/em quad range (U+2000–U+200A), line and paragraph separators, narrow no-break space, and ideographic space (U+3000).
When to reach for it
Reach for it on anything a human typed or pasted. Text copied out of Word, a PDF, a spreadsheet or a web page routinely carries non-breaking spaces at the edges, and those survive trim() untouched — which is why a "visibly empty" field passes your $value !== '' check, why a login fails on an invisible trailing character, and why two apparently identical strings compare as different.
What trips people up
The second argument is a character set, not a substring: mb_trim($s, '«»') strips any number of « and » from either end, in any order. It is not a prefix removal. Note also that supplying $characters replaces the default set rather than adding to it, so mb_trim($s, '.') will leave spaces in place.
Use mb_ltrim() or mb_rtrim() when you only want one end.
Example
When to use it
- Clean text pasted from Word or a PDF before validating that a form field is non-empty.
- Normalise CSV cells that carry ideographic spaces from a Japanese spreadsheet export.
- Strip decorative characters such as quotes or dashes from both ends of an imported title.
- Trim a user's email or username before comparing it against a stored value.
Frequently asked questions
What is the difference between trim and mb_trim?
trim() removes only six ASCII whitespace bytes, while mb_trim() removes the full Unicode whitespace set including the non-breaking space U+00A0 and the ideographic space U+3000. mb_trim() also treats the character list as characters rather than bytes.
Which PHP version added mb_trim?
PHP 8.4.0, along with mb_ltrim() and mb_rtrim(). On older versions use preg_replace('/^[\\s\\p{Z}]+|[\\s\\p{Z}]+$/u', '', $string) as a substitute.
Does the second argument of mb_trim remove a prefix?
No — it is a set of individual characters, each of which is stripped repeatedly from both ends. mb_trim('abcabc', 'abc') returns an empty string, it does not remove one 'abc'.
More examples
The invisible whitespace bug
trim() returns an 11-byte string that still starts with a non-breaking space and an ideographic space — the output only looks clean. mb_trim() returns the real 3-byte "Bö". The last two lines show a custom character set stripping repeated dashes, and leading zeros being removed from a numeric string.
<?php
$field = "\u{00A0}\u{3000}Bö\u{2009}";
var_dump(trim($field));
var_dump(mb_trim($field));
var_dump(mb_trim("--Bö--", "-"));
var_dump(mb_trim("0042", "0"));Only one end at a time
mb_rtrim drops only the trailing slash, mb_ltrim only the leading one, and mb_trim removes both. The final check shows a string of mixed ASCII and non-breaking spaces collapsing to an empty string — the emptiness test that trim() would have failed.
<?php
$path = "/données/rapports/";
echo mb_rtrim($path, "/"), "\n";
echo mb_ltrim($path, "/"), "\n";
echo mb_trim($path, "/"), "\n";
var_dump(mb_trim(" \u{00A0} ") === "");
Discussion