Home >>PHP Tutorial >PHP ltrim() Function

PHP ltrim() Function

PHP ltrim() Function

The PHP ltrim() function is used to removes whitespaces or other characters from the left side of the given input string. It is a predefined function of PHP. This function accepts two parameters out of which only one parameter is required and the other one is optional.

Syntax:-

  ltrim($string, $charlist);

Parameter Values

Parameter Description
String This is a required parameter. This parameter specify the string to be checked.
charlist This is an optional parameter. This parameter specify the character to be removed from the given string.
  • "\0" : NULL
  • "\t" : tab
  • "\n" : new line
  • " " : ordinary white space

Here is an example of ltrim() function in PHP:

<html>
<body>

<?php
$str = "!!!!!!!!!!!!!!!!!!!!!!! PHPTPOINT !";
echo "Without ltrim: ".$str . "<br>";
echo "With ltrim: " . ltrim($str,"!");
?> 

</body>
</html>
Output:
Without ltrim: !!!!!!!!!!!!!!!!!!!!!!! PHPTPOINT !
With ltrim: PHPTPOINT !

Here is an another example of ltrim() function in PHP:

<html>
<body>

<?php
$str = "     PHPTPOINT";
echo "Without ltrim: " . $str;
echo "<br>";
echo "With ltrim: " . ltrim($str);
?>
 
</body>
</html>
Output:
Without ltrim:       PHPTPOINT
With ltrim: PHPTPOINT

Here is an another example of ltrim() function in PHP:

<html>
<body>

<?php
$str = "\n\nPHPTPOINT";
echo "Without ltrim: " . $str;
echo "<br>";
echo "With ltrim: " . ltrim($str);
?>

</body>
</html>
Output:
Without ltrim:

PHPTPOINT
With ltrim: PHPTPOINT

Popular Tutorials