The example in this article describes the strange phenomenon in the use of PHP function rtrim(). Share it with everyone for your reference, the details are as follows:
I encountered a strange problem when using the rtrim()
function today:
echo rtrim('<p></p>', '</p>'); // 输出为 <p echo ltrim('www.php.cn','www.'); // 输出为 php.cn
The above output results are a bit unexpected. Originally, I thought the first line should output
, and the second line would output jb51.net.
This problem has troubled me for a long time, and I have never found the reason. Later, I found the answer in the manual:
rtrim() replaces characters in units, not strings. of. When replacing from right to left, the 6 characters of
will definitely be replaced. When going to the left, > is encountered, because > is also included in the string of the second parameter of rtirm() (< /p>), so it is also replaced. When p is encountered when going to the left, p is not included in the string of the second parameter. So the substitution stops andIf you understand it this way, the output result of the second line is expected. Haha...it's clearly written in the manual. Original text:
You can also specify the characters you want to strip, by means of the charlist parameter. Simply list all characters that you want to be stripped. With .. you can specify a range of characters .
It can be seen that the second parameter of rtrim
, ltrim
and trim
is used as a set of character lists for matching operations. . This is different from the replacement operation of the str_replace
function we know in the past.
For more articles related to strange phenomena in the use of PHP function rtrim(), please pay attention to the PHP Chinese website!