//RegexIterator 正则过滤迭代器的使用 <?php /** * 正则迭代器 * @author wolf [Qq/Email: 116311316@qq.com] * @example */ //获得递归迭代器 $test = array ('str1' => 'test 1', 'test str2' => 'another test', 'str3' => 'test 123'); //get Iterator $arrayIterator = new ArrayIterator($test); // Filter everything that starts with 'test ' followed by one or more numbers. $regexIterator = new RegexIterator($arrayIterator, '/^test (/d+)/'); // Operation mode: Replace actual value with the matches $regexIterator->setMode(RegexIterator::GET_MATCH); print_r(iterator_to_array($regexIterator)); foreach ($regexIterator as $key => $value) { // print out the matched number(s) echo $key . ' => ' . $value[1] . "/n"; }