Home > Backend Development > PHP Tutorial > How to flexibly use PHP to determine the inclusion of a string_PHP Tutorial

How to flexibly use PHP to determine the inclusion of a string_PHP Tutorial

WBOY
Release: 2016-07-15 13:31:22
Original
916 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

The above is a specific code example for PHP to judge the inclusion of a string.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446228.htmlTechArticleThe following introduces the specific use method of PHP to judge the inclusion of a string: 1. strstr: Returns a character starting from the character being judged The string to the end, if there is no return value, does not include the code as follows:...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template