php8.5
Home/ Manual/ spl / regexiterator/ RegexIterator::setFlags

RegexIterator::setFlags

PHP function Edit on GitHub ✎

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

Sets the flags

Description

RegexIterator::setFlags(int $flags): void

Sets the flags.

Parameters

flags

The flags to set, a bitmask of class constants.

The available flags are listed below. The actual meanings of these flags are described in the predefined constants.

valueconstant
1RegexIterator::USE_KEY

Return Values

Void

Examples

RegexIterator::setFlags example

Creates a new RegexIterator that filters all entries whose key starts with 'test'.

php
<?php
$test = array ('str1' => 'test 1', 'teststr2' => 'another test', 'str3' => 'test 123');

$arrayIterator = new ArrayIterator($test);
$regexIterator = new RegexIterator($arrayIterator, '/^test/');
$regexIterator->setFlags(RegexIterator::USE_KEY);

foreach ($regexIterator as $key => $value) {
    echo $key . ' => ' . $value . "\n";
}
?>

The above example will output:

output
teststr2 => another test

See Also

  • RegexIterator::getFlags

Source: reference/spl/regexiterator/setflags.xml · from the official PHP manual (php/doc-en)