下面介绍PHP判断字符串的包含的具体使用方法:
1. strstr: 返回一个从被判断字符开始到结束的字符串,如果没有返回值,则不包含
代码如下:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li><span>/*如手册上的举例*/ </span></li> <li class="alt"> <span>$</span><span class="attribute">email</span><span> = </span><span class="attribute-value">'user@example.com'</span><span>; </span> </li> <li> <span>$</span><span class="attribute">domain</span><span> = </span><span class="attribute-value">strstr</span><span>($email, '@'); </span> </li> <li class="alt"><span>echo $domain; <br>// prints @example.com </span></li> <li> <span class="tag">?></span><span> </span> </li> </ol>
2. stristr: 它和strstr的使用方法完全一样.唯一的区别是stristr不区分大小写.
3. strpos: 返回boolean值.FALSE和TRUE不用多说.用 “===”进行判断.strpos在执行速度上都比以上两个函数快,另外strpos有一个参数指定判断的位置,但是默认为空.意思是判断整个字符串.缺点是对中文的支持不好.
PHP判断字符串的包含代码如下:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute">str</span><span>= </span><span class="attribute-value">'abc'</span><span>; </span></span></li> <li> <span>$</span><span class="attribute">needle</span><span>= </span><span class="attribute-value">'a'</span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute">pos</span><span> = </span><span class="attribute-value">strpos</span><span>($str, $needle); </span> </li> </ol>
4. 用explode进行判断
PHP判断字符串的包含代码如下:
<ol class="dp-xml"> <li class="alt"><span><span>function checkstr($str){ </span></span></li> <li> <span>$</span><span class="attribute">needle</span><span> = </span><span class="attribute-value">"a"</span><span>;//判断是否包含a这个字符 </span> </li> <li class="alt"> <span>$</span><span class="attribute">tmparray</span><span> = </span><span class="attribute-value">explode</span><span>($needle,$str); </span> </li> <li> <span>if(count($tmparray)</span><span class="tag">></span><span>1){ </span> </li> <li class="alt"><span>return true; </span></li> <li><span>} else{ </span></li> <li class="alt"><span>return false; </span></li> <li><span>} </span></li> <li class="alt"><span>} </span></li> </ol>
以上就是PHP判断字符串的包含的具体代码示例。