Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:64393 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 75984 invoked from network); 20 Dec 2012 23:34:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Dec 2012 23:34:16 -0000 Authentication-Results: pb1.pair.com smtp.mail=christopher.jones@oracle.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=christopher.jones@oracle.com; 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:28762] helo=aserp1040.oracle.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 42/0F-20281-570A3D05 for ; Thu, 20 Dec 2012 18:34:14 -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 qBKNYAqi017451 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 20 Dec 2012 23:34:11 GMT Received: from acsmt356.oracle.com (acsmt356.oracle.com [141.146.40.156]) by acsinet22.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id qBKNY9Iv023357 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Thu, 20 Dec 2012 23:34:10 GMT Received: from abhmt106.oracle.com (abhmt106.oracle.com [141.146.116.58]) by acsmt356.oracle.com (8.12.11.20060308/8.12.11) with ESMTP id qBKNY9CU016228 for ; Thu, 20 Dec 2012 17:34:09 -0600 Received: from [10.191.135.142] (/10.191.135.142) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 20 Dec 2012 15:34:09 -0800 Message-ID: <50D3A06F.5000608@oracle.com> Date: Thu, 20 Dec 2012 15:34:07 -0800 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/17.0 Thunderbird/17.0 MIME-Version: 1.0 To: 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> In-Reply-To: <50D33D6B.4060404@garfieldtech.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 08:31 AM, Larry Garfield wrote: > On 12/19/12 10:30 PM, Christopher Jones wrote: >> >> >> 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? > > Eh, nothing. You're right, next() doesn't return an element, it just advances, so you still need the current() call. Which seems kinda silly to me, but whatev. That is documented, so it's OK. 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. -- christopher.jones@oracle.com http://twitter.com/ghrd