Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:64398 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 4084 invoked from network); 21 Dec 2012 06:20:30 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 21 Dec 2012 06:20:30 -0000 Authentication-Results: pb1.pair.com header.from=christopher.jones@oracle.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=christopher.jones@oracle.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain oracle.com designates 141.146.126.69 as permitted sender) X-PHP-List-Original-Sender: christopher.jones@oracle.com X-Host-Fingerprint: 141.146.126.69 aserp1040.oracle.com Received: from [141.146.126.69] ([141.146.126.69:26665] helo=aserp1040.oracle.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 75/22-20281-CAFF3D05 for ; Fri, 21 Dec 2012 01:20:29 -0500 Received: from acsinet22.oracle.com (acsinet22.oracle.com [141.146.126.238]) by aserp1040.oracle.com (Sentrion-MTA-4.2.2/Sentrion-MTA-4.2.2) with ESMTP id qBL6KODH017010 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 21 Dec 2012 06:20:24 GMT Received: from acsmt357.oracle.com (acsmt357.oracle.com [141.146.40.157]) by acsinet22.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id qBL6KNO7025795 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Fri, 21 Dec 2012 06:20:24 GMT Received: from abhmt115.oracle.com (abhmt115.oracle.com [141.146.116.67]) by acsmt357.oracle.com (8.12.11.20060308/8.12.11) with ESMTP id qBL6KN5b030277; Fri, 21 Dec 2012 00:20:23 -0600 Received: from [10.191.129.142] (/10.191.129.142) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 20 Dec 2012 22:20:23 -0800 Message-ID: <50D3FFA1.1000102@oracle.com> Date: Thu, 20 Dec 2012 22:20:17 -0800 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/17.0 Thunderbird/17.0 MIME-Version: 1.0 To: David Muir CC: internals@lists.php.net References: <50D05CD0.8080206@php.net> <50D06E8B.9020807@php.net> <50D24B4C.4070901@garfieldtech.com> <50D29483.20802@oracle.com> <50D33D6B.4060404@garfieldtech.com> <50D3A06F.5000608@oracle.com> <50D3A7DC.6070402@gmail.com> In-Reply-To: <50D3A7DC.6070402@gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Source-IP: acsinet22.oracle.com [141.146.126.238] Subject: Re: [PHP-DEV] Extension for str_replace / preg_replace with arrays From: christopher.jones@oracle.com (Christopher Jones) On 12/20/2012 04:05 PM, David Muir wrote: >> The 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( >> '/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. >> >> > > > 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()){ > $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 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.html