Home >Backend Development >PHP Tutorial >php strstr() strrchr() strpos() strrpos() function_PHP tutorial

php strstr() strrchr() strpos() strrpos() function_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:59:182069browse

These four functions are character operation functions, which mainly determine the number of times a character appears. Friends in need can refer to it.

The code is as follows
 代码如下 复制代码

//strstr:从左向右查找 返回值:字符串
//strrchr:从右向左查找 返回值:字符串
//strpos:从左向右查找 返回值:整型,假如查找的字符串不存在,则返回空
//strrpos:从右向左查找 返回值:整型
$str="天高任鸟飞,海阔凭鱼跃";//strstr:从左向右查找 strrchr:从右向左查找
echo "原始字符串:".$str."
";
echo "用strstr函数搜索“,”的返回结果:".strstr($str,",")."
";
echo "用strstr函数搜索“鸟飞”的返回结果:".strstr($str,"鸟飞")."
";

$str1="I have a great dream.";
echo "原始字符串:".$str1."
";
echo "用strrchr函数搜索“e”的返回结果:".strrchr($str1,"e")."
";
echo "用strrchr函数搜索“ea”的返回结果:".strrchr($str1,"ve")."
";

$str2="I am an abstract about abroad.";
echo "原始字符串为:".$str2."
";
echo "ab在字符串中的第一次出现位置为:".strpos($str2,"ab")."
";
echo "ab在字符串中的第一次出现位置为:".strpos($str2,"am")."
";
echo "abcd在字符串中第一次出现的位置为:".strpos($str2,"aman")."
";

$str3="I am is wang hui.";
echo "原始字符串为:".$str3."
";
echo "I在字符串中的最后一次出现位置为:".strrpos($str3,"I")."
";
echo "an在字符串中的最后一次出现位置为:".strrpos($str3,"an")."
";
echo "i在字符串中最后一次出现的位置为:".strrpos($str3,"i");

?>

Copy code
//strstr: Search from left to right Return value: string
//strrchr: Search from right to left Return value: string
//strpos: Search from left to right. Return value: integer. If the searched string does not exist, it will return empty
//strrpos: Search from right to left Return value: integer
$str="The sky is high enough for birds to fly, and the sea is wide enough for fish to jump";//strstr: Search from left to right strrchr: Search from right to left
echo "Original string: ".$str."
";
echo "The return result of searching for "," using strstr function: ".strstr($str,",")."
";
echo "The return result of searching for "bird flying" using strstr function:".strstr($str,"鸟飞")."
";

$str1="I have a great dream.";
echo "Original string:".$str1."
";
echo "The return result of searching for "e" using strrchr function: ".strrchr($str1,"e")."
";
echo "The return result of searching for "ea" using strrchr function: ".strrchr($str1,"ve")."
";

$str3="I am is wang hui.";
echo "The original string is: ".$str3."
";
echo "The last occurrence of I in the string is: ".strrpos($str3,"I")."
";
echo "The last occurrence of an in the string is: ".strrpos($str3,"an")."
";
echo "The last occurrence of i in the string is: ".strrpos($str3,"i");
?>
http://www.bkjia.com/PHPjc/631322.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631322.htmlTechArticleThese four functions are character operation functions, mainly to determine the number of times a character appears. Friends in need can refer to it. . The code is as follows Copy code ?php //strstr: Search from left to right Return...
Statement:
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