PHP ltrim() Function


The ltrim() function removes whitespaces or other predefined characters from the left side of a string.

  • rtrim() - Removes spaces or other predefined characters from the right side of a string.
  • trim() - Removes spaces or other predefined characters from both sides of a string.

Syntax
ltrim(string,charlist)
Parameters
  • string - (Required) Specify the string to check.
  • charlist - (Optional) Specifies the characters to remove from the string.
    	If the parameter is empty, the following characters are removed. 
    	"\0" – NULL 
    	"\t" – tab 
    	"\n" – line break 
    	"\x0B" – vertical tab 
    	"\r" – carriage return 
    	" " - normal whitespace
    
Example
<?php
$str = "  Hi  ,  Welcome  ";
//Remove whitespace from the beginning of the string
$txt = ltrim($str);
echo "Original string: '$str'"."<br>";
echo "ltrim string: '$txt'";
?>

Output

Original string: ' Hi , Welcome '
ltrim string: 'Hi , Welcome '

Prev Next