Signature
ltrim(string $string, string $characters = " \n\r\t\v\x00"): stringPurpose
ltrim() removes unwanted characters from the left (beginning) of a string and returns the result. By default it removes whitespace-like bytes. It is useful for cleaning user input, removing leading zeros or padding, and normalizing text before comparison. The original string is never modified, so you must use the returned value.
Signature
Parameters and defaults
$string: the input string to clean.$characters: an optional list of characters to strip. If you omit it,ltrim()strips these bytes: space, tab (\t), line feed (\n), carriage return (\r), vertical tab (\v) and the NUL byte (\0).
The second argument is a set of characters, not a prefix. Every leading character that appears anywhere in the set is removed, one after another, until a character outside the set is found. Order inside the set does not matter.
You can write an incrementing range with ... For example, "a..c" means a, b and c, and "\x00..\x1F" covers all ASCII control characters. Passing your own $characters replaces the default list entirely. If you still want whitespace removed, include it in your list.
Return value and failure cases
ltrim() always returns a string. If the whole string consists of characters from the set, the result is an empty string. It does not return false for ordinary input.
Failures are about types and malformed arguments. With declare(strict_types=1), passing a non-string raises a TypeError. Without strict types, scalar values such as integers are converted to strings. Avoid passing null, because built-in string parameters do not accept it cleanly. A malformed .. range is not treated as a range, and PHP emits a warning.
Example 1: default whitespace removal
Only leading characters are affected. Trailing whitespace stays, which is why json_encode() is handy for seeing exactly what remains.
$text = "\t Hello \n";
echo json_encode(ltrim($text), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";"Hello \n"
Example 2: removing leading zeros
A custom character list is useful for IDs and padded numbers. Note that a string made only of zeros becomes empty, so decide how your application should handle that case.
$items = ["007", "0", "000", "x007"];
$result = [];
foreach ($items as $item) {
$result[] = ltrim($item, "0");
}
echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";["7","","","x007"]
Example 3: ranges and character sets
The first call uses a range. The second shows that the argument is a set: H, e, l and l are all in "Hdle", so they are stripped, but the stripping stops at o.
$a = ltrim("abcHello", "a..c");
$b = ltrim("Hello World", "Hdle");
echo json_encode([$a, $b], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";["Hello","o World"]
Edge cases
- An empty input string returns an empty string.
- An empty
$charactersargument strips nothing, so the original text is returned unchanged. - Stripping stops at the first character not in the set, even if more matching characters appear later in the string.
- The function works on bytes. A UTF-8 non-breaking space is a multi-byte sequence and is not removed by the default list. Multibyte-aware cleanup needs a different tool, such as a regular expression with the
umodifier. - Internal whitespace is never touched. Use other functions if you need to collapse spaces in the middle of a string.
Common mistakes
- Treating the second argument as a prefix.
ltrim("pre-presentation", "pre-")does not remove just onepre-. It strips every leadingp,r,eand-, and returnssentation. To remove an exact prefix, check it withstr_starts_with()and then usesubstr(). - Ignoring the return value. Calling
ltrim($value);alone does nothing useful. Assign the result:$value = ltrim($value);. - Replacing the defaults accidentally.
ltrim($s, "0")will not remove leading spaces. Include whitespace in the list if you need both. - Expecting both ends to be cleaned.
ltrim()affects only the start. Usetrim()for both ends orrtrim()for the end. - Using it to convert numbers safely. Removing leading zeros can produce an empty string, and the result is still a string, not an integer.
Related functions
trim()strips from both the beginning and the end.rtrim()strips from the end only.str_starts_with()checks an exact prefix.substr()removes a fixed number of leading bytes.preg_replace()handles pattern-based or Unicode-aware cleanup.
Recap
ltrim() returns a copy of a string with leading characters removed. By default it strips space, tab, newline, carriage return, vertical tab and NUL. A custom list is a set of characters (with optional .. ranges), not a literal prefix. Remember to use the return value, expect an empty string when everything is stripped, and remember it works byte by byte.
Tested examples
$text = "\t Hello \n";
echo json_encode(ltrim($text), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
"Hello \n"
$items = ["007", "0", "000", "x007"];
$result = [];
foreach ($items as $item) {
$result[] = ltrim($item, "0");
}
echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
["7","","","x007"]
$a = ltrim("abcHello", "a..c");
$b = ltrim("Hello World", "Hdle");
echo json_encode([$a, $b], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
["Hello","o World"]