ltrim() function accepts a
string as its parameter, removes whitespace from the left side of that
string, and then returns it.
ltrim() can accept a second parameter containing specific characters to remove from the left side of the target
$string parameter gives the function a target string to remove whitespace or specific characters.
$characters parameter is an optional
string that specifies characters to remove.
ltrim() will start at the left side of the given
string and remove any characters that match the ones provided in the
$characters argument until it finds one that does not match.
The following example uses the
ltrim() function, first without and then with a second parameter. In the first case, without the second parameter, the
ltrim() function removes the whitespace from the left side of
" Peanut Butter". In the second case, the
ltrim() function has
"Apricot" as a second parameter, which will remove the specified characters from
"Apricot Jelly". Finally, the
echo command prints the concatenated
strings to the console:
<?php$str1 = " Peanut Butter";$str2 = "Apricot Jelly";echo ltrim($str1) . " and" . ltrim($str2, "Apricot");?>
The example will result in the following output:
Peanut Butter and Jelly
The following example is runnable and uses the
ltrim() function with both one and two arguments.
In the second call of the function, the second argument removed the characters
o, and the single space. It left the rest of the
string starting from the character
M because it wasn’t instructed by the second argument to remove that character.