ltrim – PHP String Functions
Syntax :
Description :
ltrim() function will truncate spaces or other characters from starting of input string. We can remove “hi” from the starting of input string “hi from tutorialmines.” by this function.
Parameter :
- string – This is a Required parameter. This is a string on which function will be performed.
- charlist – This is an Optional parameter. If you wont use the second parameter, ltrim() will strip below characters:
The following characters are removed if the charlist parameter is empty:
- “\0” – NULL
- “\t” – tab
- “\n” – new line
- “\r” – carriage return
- ” ” – ordinary white space
- “\x0B” – vertical tab
Output :
This will return a trimmed string.
Related articles : trim , rtrim.
ltrim() – PHP Functions Example 1 :
<?php // below statement will trim white spaces from left side of string." $strExample = " Hi from tutorialmines!"; echo ltrim($strExample); ?>
In above example ,We have a string “Hi from tutorialmines! “. Now this function will trim blank spaces from left side of string. Output of above code in the browser is as below:
ltrim() – PHP Functions Example 2 :
<?php // below statement will trim white spaces from left side of string." $strExample = " \nHi from \ntutorialmines.\n\n"; echo ltrim($strExample); ?>
In above example ,We have a string ‘ \nHi from \ntutorialmines.\n\n ‘. Now this function will trim blank spaces from left side of string. Output in ViewSource of browser is below ( HTML output of the code above):
<html><head></head><body> Hi from tutorialmines. </body></html>
See below is the output of above code.
ltrim() – PHP Functions Example 3 : Using charlist parameter.
<?php $strExample = "Hi from tutorialmines. This is a example of ltrim() function. Testing parameter."; echo ltrim($strExample,"Hi"); ?>
In above example ,We have a string ‘Hi from tutorialmines. This is a example of trim() function. Testing parameter.’. Now this function remove “Hi” from the starting of string. See below is the output of above code.
from tutorialmines. This is a example of ltrim() function. Testing parameter.
ltrim() – PHP Functions Example 4 : Using charlist parameter with (..) specifying range of characters.
<?php $strExample = "3579Hi from tutorialmines. This is a example of ltrim() function. Testing parameter."; echo ltrim($strExample,"A..Za..z0..9"); ?>
In above example ,We have a string ‘3579Hi from tutorialmines. This is a example of ltrim() function. Testing parameter.’. Now this function remove “3579Hi” character from the starting of string by using range of characters with .. operator.
- 0..9 – it will remove numbers from 0-9 from left side.
- a..z – it will remove LOWERCASE alphabets from a to z from left side.
- A..Z – it will remove UPPERCASE alphabets from A to Z from left side.
See below is the output of above code.
from tutorialmines. This is a example of ltrim() function. Testing parameter.