Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:64369 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 28790 invoked from network); 19 Dec 2012 23:18:41 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Dec 2012 23:18:41 -0000 Authentication-Results: pb1.pair.com header.from=larry@garfieldtech.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=larry@garfieldtech.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain garfieldtech.com from 66.111.4.28 cause and error) X-PHP-List-Original-Sender: larry@garfieldtech.com X-Host-Fingerprint: 66.111.4.28 out4-smtp.messagingengine.com Received: from [66.111.4.28] ([66.111.4.28:36216] helo=out4-smtp.messagingengine.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 14/E7-04341-F4B42D05 for ; Wed, 19 Dec 2012 18:18:40 -0500 Received: from compute5.internal (compute5.nyi.mail.srv.osa [10.202.2.45]) by gateway1.nyi.mail.srv.osa (Postfix) with ESMTP id BB38D20A3C for ; Wed, 19 Dec 2012 18:18:36 -0500 (EST) Received: from frontend2.nyi.mail.srv.osa ([10.202.2.161]) by compute5.internal (MEProxy); Wed, 19 Dec 2012 18:18:36 -0500 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=message-id:date:from:mime-version:to :subject:references:in-reply-to:content-type :content-transfer-encoding; s=smtpout; bh=5aKpPsRlVZvOx91ptKNLMa s73fc=; b=GCmZpoHUHd/Ii/rUl9ag3P5HcggRJd6FcVXpAP0Tb8vRcKHAyo8w29 8axDQCbwLZ4prW3Vypq0MhMrKfZqZ3Hv374BgQVK6m3JzPeH+R+5fpFl6dLz9b6w wpbCEVmvfSrgh7arUChfSIC9gMMrzU+e8KaJprdCaguOwBY3uJoUA= X-Sasl-enc: 5jnJrQ0wQ1WXE+7E0TpQ3qjXPTlzEgOcZwzPh/qupaLb 1355959116 Received: from Palantirs-MacBook-Pro.local (unknown [209.41.114.202]) by mail.messagingengine.com (Postfix) with ESMTPA id 7DCCF482562 for ; Wed, 19 Dec 2012 18:18:36 -0500 (EST) Message-ID: <50D24B4C.4070901@garfieldtech.com> Date: Wed, 19 Dec 2012 17:18:36 -0600 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; 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> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Extension for str_replace / preg_replace with arrays From: larry@garfieldtech.com (Larry Garfield) On 12/18/12 7:44 AM, Leigh wrote: > On 18 December 2012 13:24, Stefan Neufeind wrote: > >> Since we already have functionality for replacing with arrays in place, >> I wondered if giving it one string to replace and then an array to >> choose the replacement from (rotating) would be an option. Currently >> that's "unsupported" (either two strings or two arrays). >> > > It's certainly possible to implement, but personally it feels like odd > behaviour. I don't know what other people think about it. > > >> I think you could use a callback-function but would need to add quite a >> few more lines to initialise your array first, do a "next()" on the >> array inside the callback-function and (how would you pass it that >> array?) and still would have to handle starting from beginning of the >> array again once you reach the end etc. >> > > You pass the array using "use". You could do it something like this: > > $replacements = array( > 'one', 'two', 'three' > ); > > $result = preg_replace_callback( > '/word/', > function($matches) use (&$replacements) { > $current = current($replacements); > next($replacements) || reset($replacements); > return $current; > }, > 'word word word word word' > ); > > var_dump($result); > > Output: > > string(21) "one two three one two" 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