定义和用法
strstr() 函数搜索字符串在另一字符串中是否存在,如果是,返回该字符串及剩余部分,否则返回 FALSE。
注释:该函数是二进制安全的。该函数是区分大小写的。如需进行不区分大小写的搜索,请使用 stristr() 函数。
语法
strstr(string,search,before_search)
参数描述
string必需。规定被搜索的字符串。
search必需。规定要搜索的字符串。如果该参数是数字,则搜索匹配该数字对应的 ASCII 值的字符。
before_search可选。一个默认值为 "false" 的布尔值。如果设置为 "true",它将返回 search 参数第一次出现之前的字符串部分。
示例01:<?php
$str = "The scene of the women's volleyball match is so touching: she walked through the net to hold the injured and crying opponent in her arms.";
$newstr = strstr($str,"of"); //从of第一次出现的位置开始选取,到最后的字符串 strstr()函数是区分大小写的,
echo $newstr;
?>
输出:ofthe women's volleyball match is so touching: she walked through the net to hold the injured and crying opponent in her arms.
示例02:<?php
$str2 = "Select the string before the first occurrence";
//第三个参数串默认是false(选取第一次出现的位置之后的字符串);如果第三个参数改成true,(选取第一次出现的位置之前的字符串)
$gostr = strstr($str2,"the",true);
echo $gostr;
?>
输出:Select