PHP ltrim() Function

Usage — The PHP ltrim() function is used to strip/remove whitespace or other given characters from the beginning or left side of a string.

It has the following syntax:

PHP

string ltrim ( string $str [, string $character_mask ] )

Here is an example of using ltrim():

PHP

$str = "Hello World!";

// Output — " World!"
echo ltrim($str,"Hello");

// Output — "o World!"
echo ltrim($str, "Hdle");

Return Value — This function returns a string with whitespace stripped from the beginning of $str.

Additional Information — Without the second parameter, ltrim() will strip these characters: " " (an ordinary space), "\t" (a tab), "\n" (a new line), "\r" (a carriage return), "\0" (the NUL-byte) and "\x0B" (a vertical tab).

PHP Version and Changelog — The ltrim() function is available in PHP 4, PHP 5, PHP 7. The charlist parameter was added in PHP version 4.1

Relevant Functions — Other related PHP functions that you should know about are: trim() which strips whitespace (or other characters) from the beginning as well as end of a string and rtrim() which strips whitespace (or other characters) from the end of a string.

Go Directly To — usage, parameters, working examples or additional tips.

Parameters

str

The str parameter is used to specify the input string. It is a required parameter.

character_mask

The character_mask parameter is used to specify the characters that you want to strip. You juts have to list all characters that you want to be stripped. You can also use .. to specify a range of characters that you want to strip.

It is an optional parameter. When not specified the following characters are removed by default: NULL, tab, new line, vertical tab, carriage return and ordinary white space.

Working Examples

Here are some examples of using the ltrim() function:

PHP

$text = "\t\tThese are a few words :) ...  ";
$binary = "\x09Example string\x0A";
$hello  = "Hello World";

var_dump($text, $binary, $hello);
/* Output —
    string(32) "        These are a few words :) ...  "
    string(16) "    Example string
    "
    string(11) "Hello World" */

$trimmed = ltrim($text);
var_dump($trimmed);
/* Output —
    string(30) "These are a few words :) ...  " */

$trimmed = ltrim($text, " \t.");
var_dump($trimmed);
/* Output —
    string(30) "These are a few words :) ...  " */

$trimmed = ltrim($hello, "Hdle");
var_dump($trimmed);
/* Output —
    string(7) "o World" */

$clean = ltrim($binary, "\x00..\x1F");
var_dump($clean);
/* Output —
    string(15) "Example string
    " */

Additional Tips

Here are some of the most upvoted tips taken from the comment section of the PHP manual:

  1. When using a $character_mask the trimming stops at the first character that is not on that mask.
    So in the $string = "Hello world" example with $character_mask = "Hdle", ltrim($hello, $character_mask) goes like this:

    1. Check H from “Hello world” => it is in the $character_mask, so remove it
    2. Check e from “ello world” => it is in the $character_mask, so remove it
    3. Check l from “llo world” => it is in the $character_mask, so remove it
    4. Check l from “lo world” => it is in the $character_mask, so remove it
    5. Check o from “o world” => it is NOT in the $character_mask, exit the function

    Remaining string is “o world”. I hope it helps someone as I had a confusing moment with this function.

    Suggested by - tavi undersc 10 from yahoocom

  2. For those who use right-to-left languages such as Arabic, Hebrew, etc., it’s worth mentioning that ltrim() (which stands for left trim) & rtrim() (which stands for right trim) DO NOT work contextually. The nomenclature is rather semantically incorrect. So in an RTL script, ltrim() will trim text from the right direction (i.e. beginning of RTL strings), and rtrim() will trim text from the left direction (i.e. end of RTL strings).

    Suggested by - Usamah M dot Ali (usamah1228 at gmail dot com)

Further Reading

  1. You can read more about the PHP ltrim() function on PHP.net.

Reader Comments

1. You can register or login to post a comment. Asking readers to register improves the quality of discussion.

2. As a registered user, you will also get to access features like choosing between a light and dark theme or anything else that we might implement in future.

Follow Us For Updates

FacebookTwitterGoogle+
Go To Top