Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:64340 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 67397 invoked from network); 18 Dec 2012 13:24:36 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Dec 2012 13:24:36 -0000 Authentication-Results: pb1.pair.com header.from=neufeind@php.net; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=neufeind@php.net; spf=unknown; sender-id=unknown Received-SPF: unknown (pb1.pair.com: domain php.net does not designate 91.184.32.3 as permitted sender) X-PHP-List-Original-Sender: neufeind@php.net X-Host-Fingerprint: 91.184.32.3 mail.speedpartner.de Received: from [91.184.32.3] ([91.184.32.3:47271] helo=mail.speedpartner.de) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C2/D8-33799-39E60D05 for ; Tue, 18 Dec 2012 08:24:36 -0500 Received: from localhost (localhost [127.0.0.1]) by mail.speedpartner.de (Postfix) with ESMTP id 0E7A961B01 for ; Tue, 18 Dec 2012 14:24:28 +0100 (CET) Received: from mail.speedpartner.de ([127.0.0.1]) by localhost (mail.speedpartner.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id q3IV5QXJwMi9 for ; Tue, 18 Dec 2012 14:24:27 +0100 (CET) Received: from collab.speedpartner.de (collab.speedpartner.de [91.184.32.10]) by mail.speedpartner.de (Postfix) with ESMTP id EB69D60AD0 for ; Tue, 18 Dec 2012 14:24:27 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by collab.speedpartner.de (Postfix) with ESMTP id 73BB9CE191D for ; Tue, 18 Dec 2012 14:24:28 +0100 (CET) X-Virus-Scanned: amavisd-new at collab.speedpartner.de Received: from collab.speedpartner.de ([127.0.0.1]) by localhost (collab.speedpartner.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id n3LR7Y9t9Hr8 for ; Tue, 18 Dec 2012 14:24:27 +0100 (CET) Received: from [192.168.4.20] (ip-62-143-17-30.unitymediagroup.de [62.143.17.30]) by collab.speedpartner.de (Postfix) with ESMTPSA id 3BA59CE191A for ; Tue, 18 Dec 2012 14:24:27 +0100 (CET) Message-ID: <50D06E8B.9020807@php.net> Date: Tue, 18 Dec 2012 14:24:27 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/17.0 Thunderbird/17.0 MIME-Version: 1.0 To: php-dev References: <50D05CD0.8080206@php.net> In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Extension for str_replace / preg_replace with arrays From: neufeind@php.net (Stefan Neufeind) Hi, On 12/18/2012 01:16 PM, Leigh wrote: > Both str_replace and preg_replace already support some array based > replacements, I think adding more options to these functions > specifically would just lead to confusion. Well, yes and no. Currently you have to supply either one string to replace with one other or you have to supply arrays for each of them. And this wouldn't allow to replace the second, third etc. occurence different. 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). > What you're proposing can already be achieved quite easily with > preg_replace_callback and passing your array/options into the anonymous > function with `use`, I'd personally welcome a str_replace_callback > counterpart. 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. Kind regards, Stefan > On 18 December 2012 12:08, Stefan Neufeind > wrote: > > Hi, > > inside a framework-/scripting-project we've lately discussed > string-replacements with arrays. > > Currently PHP supports either replacing one string by another or > replacing first element from one array with first from another array. > > What I'd like to propose is for str_replace and preg_replace to > introduce: > * a functionality to replace one string with strings from an array > * to (optionally) allow for rolling replacements (1,2,3, 1,2,3, ...) > * to (optionally) allow to skip strings already replaced > (that means not to accidentially double-replace strings) > > This would allow to do things like > > $content = str_replace('
  • ', > array('
  • ', '
  • ', '
  • '), > $content); > > optionally starting over from A again. (Current default is to stop when > there are no more elements to replace with). > > > Would such a change/extension find support? > > > Kind regards, > Stefan Neufeind