Instance
Remove the characters to the left of the string:
<?php$str = "Hello world!"; Echo $str. "<br>"; Echo LTrim ($str, "Hello"); >
Definition and usage
The LTrim () function removes white space characters or other predefined characters to the left of the string.
Related functions:
RTrim ()-Removes the white space character or other predefined character to the right of the string.
Trim ()-removes white space characters or other predefined characters on either side of the string.
Grammar
LTrim (String,charlist)
Parameter description
string is required. Specifies the string to check.
Charlist is optional. Specifies which characters are removed from the string. If this argument is omitted, all of the following characters are removed:
"+"-NULL
"\ T"-tab
"\ n"-line break
"\X0B"-Vertical tab
"\ r"-Enter
""-Space
Technical details
Return value: Returns the modified string.
PHP version: 4+
Update log: In PHP 4.1, the charlist parameter has been added.
More examples
Example 1
To remove spaces to the left of a string:
<?php$str = "Hello world!"; echo "Without LTrim:". $str, echo "<br>", echo "with LTrim:". LTrim ($STR);? >
The HTML output of the above code is as follows (see source code):
<! DOCTYPE html>
The browser output of the above code is as follows:
Without Ltrim:hello world! With Ltrim:hello world!
Example 2
Remove the line break (\ n) to the left 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 (see source code):
<! DOCTYPE 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])-removes whitespace characters (or other characters) from the beginning and end of a string
Trim () function when the second argument is empty, the default is to remove spaces, tabs, newline characters, carriage returns, vertical tabs, and so on, when the second argument is added
1) Trim (' \ ' string\ ', ' \ ' sg '); Final output: \ "Strin
2) Trim (' \ ' string\ ', ' \ ' sg '); Final output: \ "String\"
2) Trim (' \ ' string\ ', ' \ ' sg '); Final output: Trin
The trim () function takes precedence over whitespace characters from the first and the end of the character, and then filters out the given character to be removed (list), and also applies to the LTrim (), RTrim () function