strrchr() 函数返回结果有时候不对

WBOY
Release: 2016-06-13 12:03:52
Original
1283 people have browsed it

strrchr() 函数返回结果有时不对?
手册上的经典例子:
echo strrchr("Hello world!","world");
输出为:world!

但继续尝试会发现这些奇葩结果:
echo strrchr("Hello aaaaworld!","aaaaworld");
输出:aworld!

echo strrchr("Hello cacworld!","cacworld");
输出:cworld!

echo strrchr("abc123cache123","cache");
输出:che123

echo strrchr("cache","cache");
输出:che

等等,总之是会返回不全,总是缺前面几个字符,不知为何??
------解决方案--------------------

第二个参数:
1、如果包含了不止一个字符,那么仅使用【第一个字符】
2、如果不是一个字符串,那么将被转化为整型并被视为字符顺序值
例:echo strrchr("Hello aaaaworld!","aaaaworld");
只以a为查找字符串,输出:aworld!
这是对的


------解决方案--------------------
php手册:
If needle contains more than one character, only the first is used
如果 needle 包含了不止一个字符,那么仅使用第一个字符。该行为不同于 strstr()。

w3school:
规定要查找的字符。如果该参数是数字,则搜索匹配数字 ASCII 值的字符。

显然后者忽略了“仅使用第一个字符”的说明
但“要查找的字符”和“要查找的字符串”还是有所不同的

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
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!