Home > php教程 > php手册 > php strstr() strrchr() strpos() strrpos()函数

php strstr() strrchr() strpos() strrpos()函数

WBOY
Release: 2016-06-13 10:03:47
Original
2290 people have browsed it

这四个函数是字符操作函数,主要是判断字符出现的次数,有需要的朋友可以参考一下。

 代码如下 复制代码

//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");

?>

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template