Latest web development tutorials

PHP ltrim () function

PHP String Reference PHP String Reference

Examples

Remove the left side of the character string:

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

Running instance »

Definition and Usage

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

Related functions:

  • RTRIM () - remove the right of a string of blank characters or other predefined characters.
  • the TRIM () - Remove both sides of a string of blank characters or other predefined characters.

grammar

ltrim( string,charlist )

参数 描述
string 必需。规定要检查的字符串。
charlist 可选。规定从字符串中删除哪些字符。如果省略该参数,则移除下列所有字符:
  • "\0" - NULL
  • "\t" - 制表符
  • "\n" - 换行
  • "\x0B" - 垂直制表符
  • "\r" - 回车
  • " " - 空格

technical details

return value: Returns the modified string.
PHP version: 4+
Update log: In PHP 4.1, add the charlist parameters.


More examples

Example 1

Remove string of spaces on the left side:

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

HTML output of the code above is as follows (view source):

<!DOCTYPE html>
<html>
<body>

Without ltrim: Hello World!<br>With ltrim: Hello World!
</body>
</html>

Browser output of the code above is as follows:

Without ltrim: Hello World!
With ltrim: Hello World!

Running instance »

Example 2

Remove the string to the left of line breaks (\ n):

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

HTML output of the code above is as follows (view source):

<!DOCTYPE html>
<html>
<body>

Without ltrim:


Hello World!<br>With ltrim: Hello World!
</body>
</html>

Browser output of the code above is as follows:

Without ltrim: Hello World!
With ltrim: Hello World!

Running instance »


PHP String Reference PHP String Reference