(PHP 5 >= 5.2.0, PHP 7, PHP 8)
RegexIterator::setMode — 設定操作模式
mode
操作模式。
可用的模式列於下方。這些模式的實際意義在預定義常數中說明。
值 | 常數 |
---|---|
0 | RegexIterator::MATCH |
1 | RegexIterator::GET_MATCH |
2 | RegexIterator::ALL_MATCHES |
3 | RegexIterator::SPLIT |
4 | RegexIterator::REPLACE |
無返回值。
範例 #1 RegexIterator::setMode() 範例
<?php
$test = array ('str1' => 'test 1', 'test str2' => 'another test', 'str3' => 'test 123');
$arrayIterator = new ArrayIterator($test);
// 過濾所有以 'test ' 開頭,後接一個或多個數字的項目。
$regexIterator = new RegexIterator($arrayIterator, '/^test (\d+)/');
// 操作模式:將實際值替換為匹配項
$regexIterator->setMode(RegexIterator::GET_MATCH);
foreach ($regexIterator as $key => $value) {
// 印出匹配的數字
echo $key . ' => ' . $value[1] . PHP_EOL;
}
?>
以上範例將輸出類似以下的內容
str1 => 1 str3 => 123