PHP ltrim() Function

Example

Remove characters from the left side of the string:

<?php
$str = "Hello World!";
echo $str . "<br>";
echo ltrim($str,"Hello");
?>

Run Instance

Definition and Usage

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

Related Functions:

  • rtrim() - Remove whitespace characters or predefined characters from the right side of the string
  • trim() - Remove whitespace characters or predefined characters from both sides of the string

Syntax

ltrim(string,charlist)
Parameter Description
string Mandatory. Specifies the string to be checked.
charlist

Optional. Specifies which characters to remove from the string. If this parameter is omitted, the following characters are removed:

  • "\0" - NULL
  • "\t" - Tab
  • "\n" - Newline
  • "\x0B" - Vertical Tab
  • "\r" - Carriage Return
  • " " - Space

Technical Details

Return Value: Returns the modified string.
PHP Version: 4+
Update Log: In PHP 4.1, a new feature was added: charlist Parameter.

More Examples

Example 1

Remove spaces from the left side of the string:

<?php
$str = "    Hello World!";
echo "Do not use ltrim: " . $str;
echo "<br>";
echo "Use ltrim: " . ltrim($str);
?>

The HTML output of the above code (please view the source code):

<!DOCTYPE html>
<html>
<body>
Do not use ltrim:    Hello World!<br>Use ltrim: Hello World!
</body>
</html>

The browser output of the above code:

Do not use ltrim: Hello World!
Use ltrim: Hello World!

Run Instance

Example 2

Remove newline characters (\n) from the left side of the string:

<?php
$str = "\n\n\nHello World!";
echo "Do not use ltrim: " . $str;
echo "<br>";
echo "Use ltrim: " . ltrim($str);
?>

The HTML output of the above code (please view the source code):

<!DOCTYPE html>
<html>
<body>
Do not use ltrim:
Hello World!<br>Use ltrim: Hello World!
</body>
</html>

The browser output of the above code:

Do not use ltrim: Hello World!
Use ltrim: Hello World!

Run Instance