tags:

views:

158

answers:

3

What is wrong in my code:

$i = new RegexIterator(
  new ArrayIterator(array(
    'test1'=>'test888', 
    'test2'=>'what?', 
    'test3'=>'test999')),
  '/^test(.*)/',
  RegexIterator::REPLACE);

foreach ($i as $name=>$value)
  echo $name . '=>' . $value . "\n";

The iterator is empty, why? Thanks for your help!

+1  A: 

If you ommit the operation mode (3rd parameter in your new RegexIterator statement) you'll get the matching values, like so:

$array = array('test1' => 'test888', 'test2' => 'what?', 'test3' => 'test999');
$pattern = '/^test(.*)/';

echo '<pre>';
echo "DEFAULT\n";
$arrayIterator = new ArrayIterator($array);
$regexIterator = new RegexIterator($arrayIterator, $pattern);
foreach ($regexIterator as $value) {echo "$value\n";}
echo '</pre>';

You can play with the different operation modes, depending on what you want. Go read up on the setMode documentation: http://www.php.net/manual/en/regexiterator.setmode.php

jodorovski
But if you add $regexIterator->setMode(RegexIterator::REPLACE);it returns empty iterator (as in question's example), so, it's still unclear how to use REPLACE mode.
Alexey
Yes, I can't say I have used the REPLACE mode with success. I would go with preg_replace..
jodorovski
Alexey, maybe it's a bug in PHP? I have PHP5.3
Vincenzo
hm, well I can confirm the behaviour in PHP5.2.11-0
jodorovski
reported it: http://bugs.php.net/bug.php?id=50579
Vincenzo
anyone with an interest in this topic/issue, maybe it helps to vote for this bug at http://bugs.php.net/bug.php?id=50579
jodorovski
A: 

As said already, it's a bug in PHP. I reported it to php.net: http://bugs.php.net/bug.php?id=50579

Vincenzo
A: 

RegexIterator::REPLACE is not fully implemented yet,
see http://php.net/manual/en/class.regexiterator.php

h-a-n