ltrim

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

Signature
ltrim(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 start.

ltrim walks the string from the left and drops characters as long as each one appears in the character set, stopping at the first character that does not. With no second argument it removes the six default whitespace bytes: space, tab, newline, carriage return, vertical tab and NUL.

The character set is a set, not a prefix

This is where nearly everyone gets caught. The second argument is a bag of individual characters, not a string to strip once. ltrim('Hello World', 'Hdle') returns o World, because H, e, l and l are all in the bag and get eaten one after another. If you want to remove a literal prefix, use str_starts_with plus substr, or PHP 8's preg_replace('/^prefix/', '', $s).

Common jobs

Stripping leading zeros from an identifier (ltrim($id, '0')), normalising a URL path so it never begins with a slash before you concatenate it, and cleaning control bytes out of data read from a socket or a legacy file. For the last case the range syntax helps: ltrim($line, "\x00..\x1F") removes every ASCII control character in one pass.

Remember that a string made entirely of trimmed characters comes back as an empty string — ltrim('0000', '0') is '', not '0'.

Example

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

When to use it

  • Strip leading zeros from a zero-padded order or invoice number before comparing it to a database value.
  • Normalise a URL path fragment so it never starts with a slash before joining it to a base URL.
  • Remove control bytes and BOM-style junk from the front of a line read out of a legacy import file.

Frequently asked questions

What is the difference between ltrim, rtrim and trim?

ltrim strips only from the start of the string, rtrim only from the end, and trim from both. All three take the same optional character-set argument and use the same default whitespace set.

How do I remove a specific prefix instead of a set of characters?

Use str_starts_with plus substr, or preg_replace with an anchored pattern. ltrim treats its second argument as a set of individual characters, so ltrim('http://php.net', 'http://') returns '.net' — the h, t and p at the start of the host are in that set and get eaten too.

Does ltrim remove non-breaking spaces?

No. The default set is ASCII only, and a UTF-8 non-breaking space is the two bytes \xC2\xA0. Pass it explicitly, for example ltrim($s, " \t\n\r\0\x0B\xC2\xA0"), or use preg_replace with the /u flag.

More examples

Character set, not prefix

Prints `var//log///app.log`, `name","email"` and `o World`. The first line shows only the leading run is affected — the doubled slashes in the middle survive. The last line is the classic trap: `H`, `e`, `l`, `l` are each in the character set, so four characters vanish even though `Hdle` was never meant as a prefix.

Example · php
<?php
$path = '///var//log///app.log';
echo ltrim($path, '/'), PHP_EOL;

$csvLine = "  \"name\",\"email\"";
echo ltrim($csvLine, " \""), PHP_EOL;

echo ltrim('Hello World', 'Hdle'), PHP_EOL;

Stripping control characters with a range

The first line prints `077061796c6f6164` — the default set removed the NUL byte but stopped at `\x07`, which is not whitespace. The range form `"\x00..\x1F"` covers every ASCII control character, so the second and third lines show a clean `payload`.

Example · php
<?php
$line = "\x00\x07payload";

echo bin2hex(ltrim($line)), PHP_EOL;
echo bin2hex(ltrim($line, "\x00..\x1F")), PHP_EOL;
echo ltrim($line, "\x00..\x1F"), PHP_EOL;

Discussion

  • Be the first to comment on this lesson.