The PHP function preg_grep() we introduce to you today is one of the many powerful functions in the PHP language. I hope you can have a deep understanding of this function after reading it.
Function prototype: array preg_grep (string $pattern, array $input)
PHP function preg_grep() returns an array, which includes the cells in the $input array that match the given $pattern pattern . Preg_grep() also only performs a match for each element in the input array $input. The example given in Listing 6.3 simply illustrates the use of the preg_grep() function.
PHP function preg_grep() code 6.3 Array query matching
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php </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></span></li></ol>
Through the above code example of PHP function preg_grep(), I hope it can deepen the impression for readers.