On 12/19/2012 03:18 PM, Larry Garfield wrote:
You could likely simplify the code even further using an infinite iterator:
http://us1.php.net/infiniteiterator
$result = preg_replace_callback(
'/word/',
function($matches) use (&$replacements_iterator) {
return $replacements->next();
},
'word word word word word'
);
--Larry Garfield
What am I missing that causes the first call to
$replacements_iterator->current() to return NULL
unless the iterator is rewound before use?
Chris
------------------
<?php
$replacements = array(
'one', 'two', 'three'
);
$replacements_iterator = new InfiniteIterator(new ArrayIterator($replacements));
$replacements_iterator->rewind(); // why is the rewind needed?
$result = preg_replace_callback(
'/word/',
function($matches) use ($replacements_iterator) {
$r = $replacements_iterator->current();
$replacements_iterator->next();
return $r;
},
'word word word word word'
);
var_dump($result);
// Outputs:
// string(21) "one two three one two"
// Without the call to $replacements_iterator->rewind(), the output is:
// string(18) " two three one two"
?>
--
[email protected] http://twitter.com/ghrd
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php