php strstr查找字符串中是否包含某些字符的查找函数

<p>strstr()函数的作用是:返回一个字符串在另一个字符串中首次出现的位置到后者末尾的子字符串(大小写敏感)。</p><p>strstr 定义和用法</p><p>语法</p><p>strstr(string,search,before_search)</p><p>参数<span class="Apple-tab-span" > </span>描述</p><p>string<span class="Apple-tab-span" > </span>必需。规定被搜索的字符串。</p><p>search<span class="Apple-tab-span" > </span></p><p>必需。规定所搜索的字符串。</p><p>如果此参数是数字,则搜索匹配此数字对应的 ASCII 值的字符。</p><p>before_search<span class="Apple-tab-span" > </span></p><p>可选。默认值为 &quot;false&quot; 的布尔值。</p><p>如果设置为 &quot;true&quot;,它将返回 search 参数第一次出现之前的字符串部分。</p><p>技术细节</p><p>返回值:<span class="Apple-tab-span" > </span>返回字符串的剩余部分(从匹配点)。如果未找到所搜索的字符串,则返回 FALSE。</p><p>PHP 版本:<span class="Apple-tab-span" > </span>4+</p><p>更新日志:<span class="Apple-tab-span" > </span>在 PHP 5.3 中,新增了 before_search 参数。</p><p>更多实例</p><p>例子 1</p><p>以 &quot;o&quot; 的 ASCII 值搜索字符串,并返回字符串的剩余部分:</p><pre class="brush:php;toolbar:false"> &lt;?php echostrstr(&quot;Helloworld!&quot;,111); ?&gt; 返回 oworld! 例子2 返回&quot;world&quot;第一次出现之前的字符串部分: &lt;?php echostrstr(&quot;Helloworld!&quot;,&quot;world&quot;,true); 结果: Hello</pre><p>例子3</p><p>查找 &quot;Shanghai&quot; 在 &quot;I love Shanghai!&quot; 中的第一次出现,并返回字符串的剩余部分:</p><pre class="brush:php;toolbar:false"> &lt;?php echostrstr(&quot;IloveShanghai!&quot;,&quot;Shanghai&quot;); ?&gt;</pre><p>结果:</p><p>Shanghai!</p><p>PHP 判断字符串是否包含其它字符</p><p>以下几个函数均可用来判断某字符串是否包含另外一个字符串PHP 中判断一个字符串是否包含其它字符是很常见的操作。 虽然很简单,但还是写了几个函数,质量可能不是很高,权当锻炼。 如果这几个函数恰好能帮上你的忙,我将会很高兴的。这几个函数中,我比较喜欢第四个。。。</p><pre class="brush:php;toolbar:false"> &lt;?php /** *以下几个函数均可用来判断某字符串是否包含另外一个字符串 *PHP中判断一个字符串是否包含其它字符是很常见的操作。 *虽然很简单,但还是写了几个函数,质量可能不是很高,权当锻炼。 *如果这几个函数恰好能帮上你的忙,我将会很高兴的。 */ /** *利用一下strpos()函数 *@paramunknown_type$haystack *@paramunknown_type$needle */ functionisInString1($haystack,$needle){ //防止$needle位于开始的位置 $haystack=&#39;-_-!&#39;.$haystack; return(bool)strpos($haystack,$needle); } /** *利用字符串分割 *@paramunknown_type$haystack *@paramunknown_type$needle */ functionisInString2($haystack,$needle){ $array=explode($needle,$haystack); returncount($array)&gt;1; } /** *用了一下正则,这种方法十分不建议,尤其是$needle中包含 *特殊字符,如^,$,/等等 *@paramunknown_type$haystack *@paramunknown_type$needle */ functionisInString3($haystack,$needle){ $pattern=&#39;/&#39;.$needle.&#39;/&#39;; return(bool)preg_match($pattern,$haystack); } /** *利用一下strpos()函数 *@paramunknown_type$haystack *@paramunknown_type$needle */ functionisInString4($haystack,$needle){ returnfalse!==strpos($haystack,$needle); } //测试 $haystack=&#39;IamITBDW&#39;; $needle=&#39;IT&#39;; var_dump(isInString1($haystack,$needle));</pre><p>我觉得最简单的就是这种了 strpos($a, $b) !== false 如果$a 中存在 $b,则为 true ,否则为 false。</p><p>用 !== false (或者 === false) 的原因是如果 $b 正好位于$a的开始部分,那么该函数会返回int(0),那么0是false,但$b确实位于$a中,所以要用 !== 判断一下类型,要确保是严格的 false。昨天晚上去中关村图书大厦,看到一本书中用的是 strpos === true 来判断,这是极其不正确的。。。</p><p>出错的书为《PHP求职宝典》107页(2012-02-26更新)</p><p>其它的还有 PHP 原生支持的函数,如 strstr(),stristr() 等,直接判断就可以了。</p><p>定义和用法</p><p>strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。</p><p>该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。</p><p>语法</p><p>strstr(string,search)</p><p>参数 描述</p><p>string 必需。规定被搜索的字符串。</p><p>search 必需。规定所搜索的字符串。如果该参数是数字,则搜索匹配数字 ASCII 值的字符。</p><p>提示和注释</p><p>注释:该函数是二进制安全的。</p><p>注释:该函数对大小写敏感。如需进行大小写不敏感的搜索,请使用 stristr()。</p><p>例子 1</p><pre class="brush:php;toolbar:false"> &lt;?php echostrstr(&quot;Helloworld!&quot;,&quot;world&quot;); ?&gt; //输出:world!</pre><p>例子 2</p><p>在本例中,我们将搜索 &quot;o&quot; 的 ASCII 值所代表的字符:</p><pre class="brush:php;toolbar:false"> &lt;?php echostrstr(&quot;Helloworld!&quot;,111); ?&gt; //输出:oworld!</pre><p>例子 3</p><pre class="brush:php;toolbar:false"> &lt;?php $email=&#39;admin@jb51.net&#39;; $domain=strstr($email,&#39;@&#39;); echo$domain;//prints@jb51.net $user=strstr($email,&#39;@&#39;,true);//AsofPHP5.3.0 echo$user;//printsadmin ?&gt; $city_str=fopen(cgi_path.&quot;/data/weather/city.dat&quot;,&quot;r&quot;); $city_ch=fread($city_str,filesize(cgi_path.&quot;/data/weather/city.dat&quot;)); $city_ch_arr=explode(&quot;|&quot;,$city_ch); //如果能匹配到所在市 if(strstr($area_ga,&quot;市&quot;)){ foreach($city_ch_arras$city_ch_arr_item){ if(@strstr($area_ga,$city_ch_arr_item)){ echo$area_ga.&#39;&lt;br&gt;&#39;; echo$city_ch_arr_item; $s_city=$city_ch_arr_item; } } } //如果找不到市那么看看是不是能找到省有时会有这样的情况:广东省长城宽带这样的一律归属到该省省府 elseif(strstr($area_ga,&quot;河北&quot;)!==false){ $s_city=&quot;石家庄&quot;; }elseif(strstr($area_ga,&quot;福建&quot;)!==false){ $s_city=&quot;福州&quot;; }elseif(strstr($area_ga,&quot;台湾&quot;)!==false){ $s_city=&quot;台北&quot;; }elseif(strstr($area_ga,&quot;香港&quot;)!==false){ $s_city=&quot;香港&quot;; }elseif(strstr($area_ga,&quot;广西&quot;)!==false){ $s_city=&quot;南宁&quot;; }elseif(strstr($area_ga,&quot;浙江&quot;)!==false){ $s_city=&quot;杭州&quot;; }elseif(strstr($area_ga,&quot;江苏&quot;)!==false){ $s_city=&quot;南京&quot;; }elseif(strstr($area_ga,&quot;山东&quot;)!==false){ $s_city=&quot;济南&quot;; }elseif(strstr($area_ga,&quot;安徽&quot;)!==false){ $s_city=&quot;合肥&quot;; }elseif(strstr($area_ga,&quot;湖南&quot;)!==false){ $s_city=&quot;长沙&quot;; }elseif(strstr($area_ga,&quot;四川&quot;)!==false){ $s_city=&quot;成都&quot;; }elseif(strstr($area_ga,&quot;云南&quot;)!==false){ $s_city=&quot;昆明&quot;; }elseif(strstr($area_ga,&quot;广东&quot;)!==false){ $s_city=&quot;广州&quot;; }elseif(strstr($area_ga,&quot;贵州&quot;)!==false){ $s_city=&quot;贵阳&quot;; }elseif(strstr($area_ga,&quot;西藏&quot;)!==false){ $s_city=&quot;拉萨&quot;; }elseif(strstr($area_ga,&quot;新疆&quot;)!==false){ $s_city=&quot;乌鲁木齐&quot;; }elseif(strstr($area_ga,&quot;蒙古&quot;)!==false){ $s_city=&quot;呼和浩特&quot;; }elseif(strstr($area_ga,&quot;黑龙江&quot;)!==false){ $s_city=&quot;哈尔滨&quot;; }elseif(strstr($area_ga,&quot;辽宁&quot;)!==false){ $s_city=&quot;沈阳&quot;; }elseif(strstr($area_ga,&quot;吉林&quot;)!==false){ $s_city=&quot;长春&quot;; }elseif(strstr($area_ga,&quot;河南&quot;)!==false){ $s_city=&quot;郑州&quot;; }elseif(strstr($area_ga,&quot;湖北&quot;)!==false){ $s_city=&quot;武汉&quot;; }elseif(strstr($area_ga,&quot;山西&quot;)!==false){ $s_city=&quot;太原&quot;; }elseif(strstr($area_ga,&quot;陕西&quot;)!==false){ $s_city=&quot;西安&quot;; }elseif(strstr($area_ga,&quot;甘肃&quot;)!==false){ $s_city=&quot;兰州&quot;; }elseif(strstr($area_ga,&quot;宁夏&quot;)!==false){ $s_city=&quot;银川&quot;; }elseif(strstr($area_ga,&quot;海南&quot;)!==false){ $s_city=&quot;海口&quot;; }elseif(strstr($area_ga,&quot;江西&quot;)!==false){ $s_city=&quot;南昌&quot;; }elseif(strstr($area_ga,&quot;澳门&quot;)!==false){ $s_city=&quot;澳门&quot;; } //如果都不存在就是默认显示广州比如本地机 else{ $s_city=&quot;广州&quot;; }</pre><p>如上代码:</p><p>其中 city.dat中是一些城市 格式是这样的</p><p>广州|深圳|汕头|惠州|珠海|揭阳|佛山|河源|阳江|茂名|湛江|梅州|肇庆|韶关|潮州|东莞|中山|清远|江门|汕尾|云浮|增城|从化|乐昌|南雄|台山|开平|鹤山|恩平|廉江|雷州|吴川|高州|化州|高要|四会|兴宁|陆丰|阳春|英德|连州|普宁|罗定|北京|天津|上海|重庆|乌鲁木齐|克拉玛依|石河子|阿拉尔|图木舒克|五家渠|哈密|吐鲁番|阿克苏|喀什|和田|伊宁|塔城|阿勒泰|奎屯|博乐|昌吉|阜康|库尔勒|阿图什|乌苏|拉萨|日喀则|银川|石嘴山|吴忠|固原|中卫|呼和浩特|包头|乌海|赤峰|通辽|鄂尔多斯|呼伦贝尔|巴彦淖尔|乌兰察布|霍林郭勒|满洲里|牙克石|扎兰屯|根河|额尔古纳|丰镇|锡林浩特|二连浩特|乌兰浩特|</p><p>参考</p><pre class="brush:php;toolbar:false">&lt;?php echostrstr(&#39;aaaaaaaaaaaboaaaaaaaaaaaaboxcccccccccbcccccccccccccc&#39;,&#39;box&#39;).&quot;&lt;br&gt;\n&quot;; //输出boxcccccccccbcccccccccccccc //完整匹配中间的box不因前而的b而停止 echostrstr(&#39;aaaaaaAbaaaaaaaaaaaaaaaaboxccccccccccccboxccccccccccc&#39;,&#39;box&#39;).&quot;&lt;br&gt;\n&quot;; //输出boxccccccccccccboxccccccccccc //有两个关键字时,遇到第一个停止. echostrstr(&#39;SubscrtibeourtofreenewsletteraboutNewFreewto&#39;,&#39;to&#39;).&quot;&lt;br&gt;\n&quot;; //输出tofreenewsletteraboutNewFreewto ?&gt;</pre><p>注释:该函数对大小写敏感。如需进行不区分大小写的搜索,请使用 stristr() 函数。</p>
RangeTime:0.007342s
RangeMem:215.59 KB
返回顶部 留言