Home >Backend Development >PHP Tutorial >How to flexibly use PHP to determine the inclusion of a string_PHP Tutorial
The following describes the specific use of PHP to determine the inclusion of a string:
1. strstr: Returns a string from the beginning to the end of the determined character, if there is no The return value does not include
. The code is as follows:
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php </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></span></li></ol>
2. stristr: It is used exactly the same as strstr. The only difference is that stristr is not case-sensitive.
3. strpos: Returns a boolean value. Needless to say FALSE and TRUE. Use "===" to judge. strpos is faster than the above two functions in terms of execution speed. In addition, strpos has a parameter to specify the judgment position. But the default is empty. It means to judge the entire string. The disadvantage is that the support for Chinese is not good.
The code for PHP to judge the string contains is as follows:
<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. Use explode. Judgment
The PHP code for judging the inclusion of a string is as follows:
<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>
The above is a specific code example for PHP to judge the inclusion of a string.