Re: Extension for str_replace / preg_replace with arrays
| From: | Christopher Jones | Date: | Fri, 21 Dec 2012 06:20:17 +0000 |
| Subject: | Re: Extension for str_replace / preg_replace with arrays | ||
| References: | 1 2 3 4 5 6 7 8 9 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-64398@lists.php.net to get a copy of this message | ||
On 12/20/2012 04:05 PM, David Muir wrote:
I logged a bug so this can be tracked and re-discovered: https://bugs.php.net/bug.php?id=63823 Chris -- christopher.jones@oracle.com http://twitter.com/ghrd Newly updated, free PHP & Oracle book: http://www.oracle.com/technetwork/topics/php/underground-php-oracle-manual-098250.htmlThe curiosity (bug?) is the need to call rewind(): $replacements_iterator = new InfiniteIterator(new ArrayIterator($replacements)); $replacements_iterator->rewind(); // why is the rewind needed? $result = preg_replace_callback(What happens if you do: $replacements_iterator = new InfiniteIterator(new ArrayIterator($replacements)); var_dump($replacements_iterator->current()); If I remember correctly, when you pass a traversable into foreach, under the hood, it basically calls: $iterator->rewind(); while($iterator->valid()){'/word/', function($matches) use ($replacements_iterator) { $r = $replacements_iterator->current(); $replacements_iterator->next(); return $r; }, 'word word word word word word word word'); In other (simple) scripts using InfiniteIterator the rewind wasn't needed.$value = $iterator->current(); ....(foreach block).... $iterator->next();} So that might explain why it works in "(simple) scripts". It does seem like a bug that the rewind is required though. A newly created iterator should already be in a rewound state so the call shouldn't be needed. Cheers, David