PHP ltrim()Function
Examples
Remove characters from the left side of a string:
<?php
$str = "Hello World!";
echo $str . "<br>";
echo ltrim($str,"Hello");
?>
$str = "Hello World!";
echo $str . "<br>";
echo ltrim($str,"Hello");
?>
Run example »
Definition and Usage
The ltrim() function removes whitespace or other predefined characters from the left side of a string.
Related functions:
- rtrim()- Removes whitespace or other predefined characters from the right side of a string.
- trim() - Removes whitespace or other predefined characters from both sides of a string.
Syntax
ltrim(string,charlist)
| Parameters | Description |
|---|---|
| string | Required. Specifies the string to check. |
| charlist | Optional. Specifies which characters to remove from the string. If this parameter is omitted, all of the following characters are removed:
|
Technical Details
| Return value: | Returns the modified string. |
|---|---|
| PHP version: | 4+ |
| Changelog: | In PHP 4.1, thecharlistparameter was added. |
More Examples
Example 1
Remove spaces from the left side of a string:
<?php
$str = " Hello World!";
echo "Without ltrim: " . $str;
echo "<br>";
echo "With ltrim: " . ltrim($str);
?>
$str = " Hello World!";
echo "Without ltrim: " . $str;
echo "<br>";
echo "With ltrim: " . ltrim($str);
?>
The HTML output of the above code is as follows (view source):
<!DOCTYPE html>
<html>
<body>
Without ltrim: Hello World!<br>With ltrim: Hello World!
</body>
</html>
<html>
<body>
Without ltrim: Hello World!<br>With ltrim: Hello World!
</body>
</html>
The browser output of the above code is as follows:
Without ltrim: Hello World!
With ltrim: Hello World!
With ltrim: Hello World!
Run example »
Example 2
Remove newlines (\n) from the left side of a string:
<?php
$str = "nnnHello World!";
echo "Without ltrim: " . $str;
echo "<br>";
echo "With ltrim: " . ltrim($str);
?>
$str = "nnnHello World!";
echo "Without ltrim: " . $str;
echo "<br>";
echo "With ltrim: " . ltrim($str);
?>
The HTML output of the above code is as follows (view source):
<!DOCTYPE html>
<html>
<body>
Without ltrim:
Hello World!<br>With ltrim: Hello World!
</body>
</html>
<html>
<body>
Without ltrim:
Hello World!<br>With ltrim: Hello World!
</body>
</html>
The browser output of the above code is as follows:
Without ltrim: Hello World!
With ltrim: Hello World!
With ltrim: Hello World!
Run example »
PHP String Reference Manual Other Extensions