PHP ltrim() Function
Example
Remove characters from the left side of the string:
<?php $str = "Hello World!"; echo $str . "<br>"; echo ltrim($str,"Hello"); ?>
Definition and Usage
The ltrim() function removes whitespace characters or predefined characters from the left side of the string.
Related Functions:
Syntax
ltrim(string,charlist)
Parameter | Description |
---|---|
string | Mandatory. Specifies the string to be checked. |
charlist |
Optional. Specifies which characters to remove from the string. If this parameter is omitted, the following characters are removed:
|
Technical Details
Return Value: | Returns the modified string. |
PHP Version: | 4+ |
Update Log: | In PHP 4.1, a new feature was added: charlist Parameter. |
More Examples
Example 1
Remove spaces from the left side of the string:
<?php $str = " Hello World!"; echo "Do not use ltrim: " . $str; echo "<br>"; echo "Use ltrim: " . ltrim($str); ?>
The HTML output of the above code (please view the source code):
<!DOCTYPE html> <html> <body> Do not use ltrim: Hello World!<br>Use ltrim: Hello World! </body> </html>
The browser output of the above code:
Do not use ltrim: Hello World! Use ltrim: Hello World!
Example 2
Remove newline characters (\n) from the left side of the string:
<?php $str = "\n\n\nHello World!"; echo "Do not use ltrim: " . $str; echo "<br>"; echo "Use ltrim: " . ltrim($str); ?>
The HTML output of the above code (please view the source code):
<!DOCTYPE html> <html> <body> Do not use ltrim: Hello World!<br>Use ltrim: Hello World! </body> </html>
The browser output of the above code:
Do not use ltrim: Hello World! Use ltrim: Hello World!