(PHP 5 >= 5.2.0, PHP 7, PHP 8)
RegexIterator::__construct — 建立新的 RegexIterator
$iterator
,$pattern
,$mode
= RegexIterator::MATCH,$flags
= 0,$pregFlags
= 0建立一個新的 RegexIterator,它使用正規表達式過濾 Iterator。
iterator
要套用此正規表達式過濾器的迭代器。
pattern
要匹配的正規表達式。
mode
操作模式,請參見 RegexIterator::setMode() 以取得模式列表。
flags
特殊旗標,請參見 RegexIterator::setFlags() 以取得可用旗標列表。
pregFlags
正規表達式旗標。這些旗標取決於操作模式參數
操作模式 | 可用旗標 |
---|---|
RegexIterator::ALL_MATCHES | 請參見 preg_match_all()。 |
RegexIterator::GET_MATCH | 請參見 preg_match()。 |
RegexIterator::MATCH | 請參見 preg_match()。 |
RegexIterator::REPLACE | 無。 |
RegexIterator::SPLIT | 請參見 preg_split()。 |
如果 pattern
參數無效,則會拋出 InvalidArgumentException。
範例 #1 RegexIterator::__construct() 範例
建立一個新的 RegexIterator,它會過濾所有以 'test' 開頭的字串。
<?php
$arrayIterator = new ArrayIterator(array('test 1', 'another test', 'test 123'));
$regexIterator = new RegexIterator($arrayIterator, '/^test/');
foreach ($regexIterator as $value) {
echo $value . "\n";
}
?>
上述範例將輸出類似以下的內容
test 1 test 123