0bc09.php
来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 30 行
PHP
30 行
<?php// Declare a new class that extends FilterIterator into a Regex one.class RegexFilterIterator extends FilterIterator { // Declare a property to hold the filter criteria private $criteria; // Now make our constructor, it needs to take an Iterator, and the regex public function __construct(Iterator $i, $regex) { // Call the parents constructor, and store the regex parent::__construct($i); $this->criteria = $regex; } // Define accept to do the regex check public function accept() { // Just see if the regex matches the current item: return preg_match($this->criteria, parent::current()); }}// Now to test this, first define an array of values$arr = array('Mimi', 'Wiley', 'Tux', 'Taz', 'Iyi', 'Thundar', 'Yowler');// Now let's create a chained Iterator. Make a FilterIterator that wraps// around an ArrayIterator, and only allows items with a 'y' in them.$it = new RegexFilterIterator(new ArrayIterator($arr), '/y/i');// Now loop and echo these all out, should include Wiley, Iyi, Yowler.foreach ($it as $entry) { echo "{$entry}<br />\n"; }?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?