Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-13 11:07:57
Original
1305 people have browsed it

我们今天为大家介绍的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>
Copy after login

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


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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!