PHP ltrim() Function
Example
Remove characters from the left side of a string:
<?php
$str = "Hello World!";
echo $str . "<br>";
echo ltrim($str,"Hello");
?>
Try it Yourself »
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)
Parameter Values
Parameter | Description |
---|---|
string | Required. Specifies the string to check |
charlist | Optional. Specifies which characters to remove from the string. If omitted, all of the following characters are removed:
|
Technical Details
Return Value: | Returns the modified string |
---|---|
PHP Version: | 4+ |
Changelog: | The charlist parameter was added in PHP 4.1 |
More Examples
Example
Remove whitespaces from the left side of a string:
<?php
$str = " Hello World!";
echo "Without ltrim: " . $str;
echo "<br>";
echo "With ltrim: " . ltrim($str);
?>
The HTML output of the code above will be (View Source):
<!DOCTYPE html>
<html>
<body>
Without ltrim: Hello World!<br>With ltrim: Hello World!
</body>
</html>
The browser output of the code above will be:
Without ltrim: Hello World!
With ltrim: Hello World!
Try it Yourself »
Example
Remove newlines (\n) from the left side of a string:
<?php
$str = "\n\n\nHello World!";
echo "Without ltrim: " . $str;
echo "<br>";
echo "With ltrim: " . ltrim($str);
?>
The HTML output of the code above will be (View Source):
<!DOCTYPE html>
<html>
<body>
Without ltrim:
Hello World!<br>With ltrim: Hello World!
</body>
</html>
The browser output of the code above will be:
Without ltrim: Hello World!
With ltrim: Hello World!
Try it Yourself »
❮ PHP String Reference
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.