PHP ltrim function
The PHP ltrim function can be used to delete the blank characters at the beginning of the string, including "normal blank, \t, \n, \0, \x0B" and other characters. In PHP 4.1.0 and newer versions, the ltrim function The formula also adds the function of setting other character parameters, which can be used to delete more characters at the beginning of the string. If you want to delete suffix blanks, please use the rtrim function corresponding to ltrim .
Basic syntax of PHP ltrim function
The simplest method of the ltrim function only needs to use the first parameter $str. $str represents the string to be processed, which is required. The second parameter $charlist is PHP 4.1.0. Add another Character deletion function. This is an optional item. If the second parameter is not used, ltrim will only delete the following characters.
- "" ( ASCII 32 (0x20))
- "\t" ( ASCII 9 (0x09))
- "\n" ( ASCII 10 (0x0A))
- "\r" ( ASCII 13 (0x0D))
- "\0" ( ASCII 0 (0x00))
- "\x0B" ( ASCII 11 (0x0B))
To delete other characters at the beginning, please use the second parameter $charlist of the ltrim function to set it.
PHP ltrim function example
$text_1 = "This is a test string";
$text_2 = "a This is a test string";
$text_1 = ltrim ($text_1);
var_dump($text_1);
echo'<br>';
$text_2 = ltrim ($text_2,'a');
var_dump($text_2);
?>
The above example output
string(18) "This is the test string"
We first prepared two strings, which start with ordinary blanks and the English letter "a". The first time you use the ltrim function, you do not need to use the second parameter to smoothly remove the ordinary blanks at the beginning of the string. The second time I use the ltrim function, the English letter a is not deleted by default, so I used the second parameter to delete the letter a smoothly.
Other supplements, var_dump in the example is a function used to print variables on the screen. It will output the length and content of the string at the same time, while "echo'<br>';" will output newline characters, so the example The result shows the effect of two lines.
Post a Comment
0 Comments