PHP ltrim

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP : ltrim() function

Description

The ltrim() function is used to remove white spaces or other predefined characters form the left side of the string.

Version

(PHP 4 and above)

Syntax

ltrim(string_name, char_list)

Parameter

Name

Description

Required /
Optional

Type

string_name

The string that will be trimmed.

Required

String

char_list

Specifies which character to remove from the string. Without this parameter, the function removes the following characters. 
' ' an ordinary space.
"�" - the NUL-byte.
" " - tab.
" " - line feed.
"x0B" - a vertical tab.
" " - a carriage return.

Optional

String

Return value

This function returns a string with whitespace stripped from the beginning of string_name. Without the second parameter, ltrim() will strip these characters:

Value Type : String.

Pictorial Presentation

php-string-ltrim()

Example:

  1. $text1="w3resource.com  ";  

  2. $text2=" w3resource.com";  

  3. $text3=" w3resource.comx0A";  

  4. $text4="Good Morning";  

  5. $trimmed_text=ltrim($text1);  

  6. var_dump($trimmed_text);  

  7. echo '
    ';  

  8. $trimmed_text=ltrim($text2);  

  9. var_dump($trimmed_text);  

  10. echo '
    ';  

  11. $trimmed_text=ltrim($text3);  

  12. var_dump($trimmed_text);  

  13. echo '
    ';  

  14. $trimmed_text=ltrim($text1,'w3');  

  15. var_dump($trimmed_text);  

  16. echo '
    ';  

  17. $trimmed_text=ltrim($text4,"Go");  

  18. var_dump($trimmed_text);  

  19. ?>  

Output :

string(16) "w3resource.com " 
string(14) "w3resource.com" 
string(15) "w3resource.com " 
string(14) "resource.com " 
string(9) "d Morning"