I was testing the strrchr function and was baffled by its behaviors. It is supposed to return the substring from the last occurrence of the matching pattern. For instance,
$test = 'example@hotmail.com';
echo strrchr($test,'@') ;开发者_运维技巧
this returns the substring from the "@", that is @hotmail.com, but if I search for this "hot"
$test = 'example@hotmail.com';
echo strrchr($test,'example') ;
instead of tmail.com (I expected), how come it returns hotmail.com, and if I search for other patterns, the results are strange too. Thank you
Whenever programming, be sure to check the documentation. The strrchr() function does not do what you think it does:
This function returns the portion of haystack which starts at the last occurrence of needle and goes until the end of haystack.
If needle contains more than one character, only the first is used. This behavior is different from that of strstr().
You probably wanted the strstr() function in the first place.
Only first character is used for searching, based on php documentation:
If needle contains more than one character, only the first is used. This behavior is different from that of strstr().
see: http://php.net/manual/en/function.strrchr.php
精彩评论