PHP ltrim() function

The ltrim() function is used to remove white space and other predefined characters from beginning of a string.

Syntax

ltrim(  $string , $character) : string

Parameters

Parameter Name Descrption
$string Required, String to be remove whitespace/predefined character
$character

Optional, The ltrim() function return the string with whitespace removed, if second parameter will not passed then ltrim() will strip these characters:
" " - an ordinary space
"\t" - a tab
"\n" - new line
"\r" - carriage return
"\0" - NUL-byte
"\x0B" - vertical tab.

Return Value

The trimmed string

ltrim() examples

$string1 = " this is string"; //length : 16
$ltrim_string1 = ltrim($string1);
var_dump($ltrim_string1);
//output : 'this is string' (length=14)

$text_ltrim =  "\t\tThis is string ";
var_dump($text_ltrim); 
//output : '		This is string ' (length=17)

$trimmed = ltrim($text_ltrim);
var_dump($trimmed);
// output : 'This is string ' (length=15)

$text_ltrim =  "ltrim function in PHP";
$trimmed = ltrim($text_ltrim,'l');
var_dump($trimmed);
// output : 'trim function in PHP' (length=20)

Related Functions

  • trim(): Strip whitespace (or other characters) from the beginning and end of a string
  • rtrim(): Strip whitespace (or other characters) from the end of a string