mb_ltrim

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

Signature
mb_ltrim(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 beginning.

Errors

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

mb_ltrim() removes characters from the front of a string, and unlike ltrim() it reasons in characters rather than bytes. That difference shows up in two ways.

First, the default whitespace set is much larger. ltrim() knows about six ASCII characters — space, tab, newline, carriage return, NUL and vertical tab. mb_ltrim() knows the full Unicode set: the no-break space U+00A0 that Word and Outlook paste in, the ideographic space U+3000 common in Japanese and Chinese text, the en and em spaces of the U+2000 block, plus the line and paragraph separators. Text pasted from a word processor routinely carries these, and trim() leaves every one of them in place.

Second, a custom characters list is read as characters. ltrim() treats that argument as a bag of bytes, so removing a multi-byte character can chop a neighbouring character in half and leave you with an invalid string. mb_ltrim() matches whole characters only, so it either removes one or stops.

Two things it does not do: the byte-order mark U+FEFF is not whitespace and is not in the default set, so list it explicitly if you are stripping BOMs; and there is no range syntax, so each character to strip must be written out in full.

Example

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

When to use it

  • Clean text pasted from Word that begins with a no-break space.
  • Strip a leading ideographic space from Japanese or Chinese form input.
  • Remove leading zeros from a reference number without touching the rest.
  • Drop a leading BOM plus whitespace from the first line of an imported file.

Frequently asked questions

What is the difference between ltrim and mb_ltrim?

ltrim() works on bytes and only knows six ASCII whitespace characters, while mb_ltrim() works on characters and strips the full Unicode whitespace set. A custom character list is also matched per character rather than per byte, so mb_ltrim() cannot leave a half-truncated character behind.

How do I remove a non-breaking space from the start of a string?

mb_ltrim($string) removes U+00A0 automatically, since it is part of the default Unicode whitespace set. On PHP 8.3 and earlier you need something like preg_replace('/^[\s\x{00A0}]+/u', '', $string).

Which PHP version added mb_ltrim?

PHP 8.4.0, together with mb_trim() and mb_rtrim(). Before that the usual workaround was a PCRE pattern with the /u modifier.

More examples

Byte-based trimming can corrupt a string

ltrim() treats "本" as the three separate bytes e6, 9c and ac. The first byte of 日 is also e6, so it is stripped, leaving a truncated character and a string that no longer validates as UTF-8. mb_ltrim() compares whole characters, sees that the string does not start with 本, and returns it untouched.

Example · php
<?php
$s = "日本語";

var_dump(bin2hex($s));
var_dump(bin2hex(ltrim($s, "本")));   // byte set: e6, 9c, ac
var_dump(mb_ltrim($s, "本"));
var_dump(mb_check_encoding(ltrim($s, "本"), 'UTF-8'));

Strip a BOM and whitespace from pasted input

The default call stops at the BOM, because U+FEFF is a zero-width no-break space by name but is not classified as whitespace. Listing it explicitly alongside the space and the NBSP removes all three and leaves "Order #42" at nine bytes.

Example · php
<?php
$pasted = "\u{FEFF}\u{00A0}  Order #42";

echo strlen($pasted), " bytes in\n";
var_dump(mb_ltrim($pasted));                    // BOM is not whitespace

$clean = mb_ltrim($pasted, "\u{FEFF} \u{00A0}");
var_dump($clean);
echo strlen($clean), " bytes out\n";

Trim a set of characters, not a prefix

The characters argument is a set, not a prefix: every leading character that appears anywhere in the set is removed, in any order and any number of times, which is why "abcabcTAIL" loses six characters. Note the trailing zeros in the first line survive — only the start of the string is touched.

Example · php
<?php
echo mb_ltrim("0001200", "0"), "\n";
echo mb_ltrim("«»«quote", "«»"), "\n";
echo mb_ltrim("abcabcTAIL", "abc"), "\n";
echo mb_ltrim("---dash", "-"), "\n";

Discussion

  • Be the first to comment on this lesson.