Heim  >  Artikel  >  Backend-Entwicklung  >  示例讲解PHP函数preg_grep()的使用_PHP教程

示例讲解PHP函数preg_grep()的使用_PHP教程

WBOY
WBOYOriginal
2016-07-15 13:30:571967Durchsuche

我们今天为大家介绍的PHP函数preg_grep()就是PHP语言中,众多功能强大的函数之一,希望大家在阅读后能够对这一函数优异个深刻的认识。

函数原型:array preg_grep (string $pattern, array $input)
PHP函数preg_grep()返回一个数组,其中包括了$input数组中与给定的$pattern模式相匹配的单元。对于输入数组$input中的每个元素,preg_grep()也只进行一次匹配。代码6.3给出的示例简单地说明了preg_grep()函数的使用。

PHP函数preg_grep()代码6.3 数组查询匹配

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php  </span></span></span></li>
<li>
<span>$</span><span class="attribute">subjects</span><span> = </span><span class="attribute-value">array</span><span>(  </span>
</li>
<li class="alt"><span>"Mechanical Engineering", "Medicine",  </span></li>
<li><span>"Social Science", "Agriculture",  </span></li>
<li class="alt"><span>"Commercial Science", "Politics"  </span></li>
<li><span>);  </span></li>
<li class="alt"><span>//匹配所有仅由有一个单词组成的科目名  </span></li>
<li>
<span>$</span><span class="attribute">alonewords</span><span> = </span><span class="attribute-value">preg_grep</span><span>("/^[a-z]*$/i"<br>, $subjects);  </span>
</li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
</ol>

通过以上这段PHP函数preg_grep()的代码示例,希望能够为读者起到一个加深印象的作用。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446250.htmlTechArticle我们今天为大家介绍的PHP函数preg_grep()就是PHP语言中,众多功能强大的函数之一,希望大家在阅读后能够对这一函数优异个深刻的认识。 函...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn