Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:64371 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 46306 invoked from network); 20 Dec 2012 04:31:09 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Dec 2012 04:31:09 -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:35379] helo=aserp1040.oracle.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 9E/79-04341-B8492D05 for ; Wed, 19 Dec 2012 23:31:07 -0500 Received: from ucsinet21.oracle.com (ucsinet21.oracle.com [156.151.31.93]) by aserp1040.oracle.com (Sentrion-MTA-4.2.2/Sentrion-MTA-4.2.2) with ESMTP id qBK4V2IC010163 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 20 Dec 2012 04:31:03 GMT Received: from acsmt357.oracle.com (acsmt357.oracle.com [141.146.40.157]) by ucsinet21.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id qBK4V2Ck017626 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Thu, 20 Dec 2012 04:31:02 GMT Received: from abhmt106.oracle.com (abhmt106.oracle.com [141.146.116.58]) by acsmt357.oracle.com (8.12.11.20060308/8.12.11) with ESMTP id qBK4V1QZ018951 for ; Wed, 19 Dec 2012 22:31:02 -0600 Received: from [10.191.135.165] (/10.191.135.165) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 19 Dec 2012 20:31:01 -0800 Message-ID: <50D29483.20802@oracle.com> Date: Wed, 19 Dec 2012 20:30:59 -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> In-Reply-To: <50D24B4C.4070901@garfieldtech.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Source-IP: ucsinet21.oracle.com [156.151.31.93] Subject: Re: [PHP-DEV] Extension for str_replace / preg_replace with arrays From: christopher.jones@oracle.com (Christopher Jones) 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 ------------------ 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" ?> -- christopher.jones@oracle.com http://twitter.com/ghrd