Some abnormal phenomena during the use of php rtrim() function

怪我咯
Release: 2023-03-12 15:28:02
Original
1742 people have browsed it

This article mainly introduces the weird phenomena in the use of PHP function rtrim(), and analyzes the problems that occur in the character matching process of PHP function rtrim based on specific examples. and solutions will help to further understand the principles and usage techniques of the rtrim function. Friends in need can refer to

. This article describes the weird phenomena in the use of the 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(&#39;<p></p>&#39;, &#39;</p>&#39;); // 输出为 <p
echo ltrim(&#39;www.php.cn&#39;,&#39;www.&#39;); // 输出为 php.cn
Copy after login

The above output result is 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 String. 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 and

If 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 <a href="http://www.php.cn/wiki/1372.html" target="_blank">str_replace</a> function we have known before.

The above is the detailed content of Some abnormal phenomena during the use of php rtrim() function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!