Home >Backend Development >PHP Tutorial >PHP removes whitespace characters or other predefined characters on the left side of the string function ltrim()

PHP removes whitespace characters or other predefined characters on the left side of the string function ltrim()

黄舟
黄舟Original
2017-11-02 13:14:422607browse

Example

Remove characters on the left side of the string:

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

Definition and usage

ltrim() function removes blank characters or other characters on the left side of the string Predefined characters.

Related functions:

rtrim() - Removes whitespace characters or other predefined characters on the right side of a string.

trim() - Removes whitespace characters or other predefined characters on both sides of a string.

Syntax

ltrim(string,charlist)

Parameters             Description

string                                                              Specifies the string to check. ​

charlist ​ Optional. Specifies which characters are removed from a string. If this parameter is omitted, remove all the following characters:

"\ 0" -NULL

## "\ T" -The watch symbol

## "\ n"

"\ x0b" - Vertical forms

"\ r" -Elke

## "" " - 空

#Technical details

Return value: Return the modified string.

PHP Version: 4+

##Change Log: In PHP 4.1, new Added charlist parameter.

More examples

Example 1

Remove spaces on the left side of the string:

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

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

<!DOCTYPE html>
<html>
<body>

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

The browser output of the above code is as follows:

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

Example 2

Remove the newline character (\n) on the left side of the string :

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

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

<!DOCTYPE html>
<html>
<body>

Without ltrim: 


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

The browser output of the above code is as follows:

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

string trim ( string $str [, string $charlist ] ) - 去除字符串首尾处的空白字符(或者其他字符)

  trim()函数当第二个参数为空时,默认去掉空格、制表符、换行符、回车符、垂直制表符等,当加入第二个参数时

    1) trim('   \"string\"', '\"sg');   // 最终输出:\"strin

    2) trim('   \"string\"   ', '\"sg');   // 最终输出:\"string\"

    2)trim('\"string\"', '\"sg');    // 最终输出:trin

  所以trim()函数优先去掉字符首尾的空白字符,再过滤掉给定的要去除的字符(列表),也适用于ltrim()、rtrim()函数

The above is the detailed content of PHP removes whitespace characters or other predefined characters on the left side of the string function ltrim(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn