PHP strrpos strpos strstr strrchr 区别

1. strstr

string strstr ( string $haystack , mixed$needle [, bool $before_needle = false ] )

$needle 为字符串,如果不是字符串,那么转化为整型,使用该整型对应的字符。

返回 该字符(串)首次出现到字符串尾部分, 包括该字符(串)。

2. strrchr

string strrchr ( string $haystack , mixed$needle )

$needle为字符, 如果是字符串,使用字符串的首字符作为匹配字符(区别于其他几个函数对字符串情况的处理), 如果不是字符也不是字符串,则转化为整型,使用该整型对应的字符。

返回该字符最后一次出现到字符串尾部分, 包括该字符。

3. strpos

int strpos ( string $haystack , mixed$needle [, int $offset = 0 ] )

返回某个字符串第一次出现的位置, 如果 needle 不是一个字符串,那么它将被转换为整型并被视为字符的顺序值。

4. strrpos

返回某个字符最后一次出现的位置, 如果 needle 不是一个字符串,那么它将被转换为整型并被视为字符的顺序值。

<?php

$str = 'sailrancho@qq.com';

echo strstr($str,'qq.'); // 返回 qq.com
echo "\n";
echo strrchr($str, 'ch'); // 返回 com 注意返回不是cho@qq.com
echo "\n";
echo strpos($str, 'c'); //返回 7 
echo "\n";
echo strrpos($str, 'c'); // 返回14