w3resource

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.
"\0" - the NUL-byte.
"\t" - tab.
"\n" - line feed.
"\x0B" - a vertical tab.
"\r" - 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:

<?php
$text1="w3resource.com  ";
$text2="\t\tw3resource.com";
$text3="\t\tw3resource.com\x0A";
$text4="Good Morning";
$trimmed_text=ltrim($text1);
var_dump($trimmed_text);
echo '<br>';
$trimmed_text=ltrim($text2);
var_dump($trimmed_text);
echo '<br>';
$trimmed_text=ltrim($text3);
var_dump($trimmed_text);
echo '<br>';
$trimmed_text=ltrim($text1,'w3');
var_dump($trimmed_text);
echo '<br>';
$trimmed_text=ltrim($text4,"Go");
var_dump($trimmed_text);
?>

Output :

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

View the example in the browser

See also

PHP Function Reference

Previous: join
Next: md5_file



PHP: Tips of the Day

In PHP, there are two versions of logical AND and OR operators.

Operator True if
$a and $b Both $a and $b are true
$a && $b Both $a and $b are true
$a or $b Either $a or $b is true
$a || $b Either $a or $b is true

Note that the && and || opererators have higher precedence than and and or. See table below:

Evaluation Result of $e Evaluated as
$e = false || true True $e = (false || true)
$e = false or true False ($e = false) or true

Because of this it's safer to use && and || instead of and and or.