Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:64341 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 69714 invoked from network); 18 Dec 2012 13:44:45 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Dec 2012 13:44:45 -0000 Authentication-Results: pb1.pair.com smtp.mail=leight@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=leight@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.210.178 as permitted sender) X-PHP-List-Original-Sender: leight@gmail.com X-Host-Fingerprint: 209.85.210.178 mail-ia0-f178.google.com Received: from [209.85.210.178] ([209.85.210.178:58478] helo=mail-ia0-f178.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A0/39-33799-B4370D05 for ; Tue, 18 Dec 2012 08:44:44 -0500 Received: by mail-ia0-f178.google.com with SMTP id k25so514065iah.23 for ; Tue, 18 Dec 2012 05:44:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=13cKVG3k7RKNWCumOtQe6uPXTxIe7dqCqNw0S0upa88=; b=dk1+bx2T9visi07WILEvLE0deyE4J+JlRLuiamX6QEuUITE7iwrVhSkbbJiB4rXyNa P8wGWpC0vA0ZVTU7TZi/vFV6ABZ0EUzVy03tVQZTW8g6f1fouJzj/4BeEDwZB8kebcSJ XN1eQE1lNx6KMBEq4EJ2TnDp2ctWzR/LA9mcepB077EoQC54juXoWxHWOkkfXI8lPRDR +xEkGNRthLCL0+PeSM7rv2nqM6938f+igYUbvBWDkV2xYm5/mutWS131ACjTWRwB1WWn G14ZF3Ef+IFtofBC0+fT69x2Adggh0b5v+9SQx/tCpi4lhhPe7469FUhJr6WAw7HKVB9 n5mw== MIME-Version: 1.0 Received: by 10.43.4.70 with SMTP id ob6mr1954972icb.56.1355838280494; Tue, 18 Dec 2012 05:44:40 -0800 (PST) Received: by 10.64.68.81 with HTTP; Tue, 18 Dec 2012 05:44:40 -0800 (PST) In-Reply-To: <50D06E8B.9020807@php.net> References: <50D05CD0.8080206@php.net> <50D06E8B.9020807@php.net> Date: Tue, 18 Dec 2012 13:44:40 +0000 Message-ID: To: Stefan Neufeind Cc: php-dev Content-Type: multipart/alternative; boundary=bcaec50fe22714ede704d120b3b0 Subject: Re: [PHP-DEV] Extension for str_replace / preg_replace with arrays From: leight@gmail.com (Leigh) --bcaec50fe22714ede704d120b3b0 Content-Type: text/plain; charset=ISO-8859-1 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" --bcaec50fe22714ede704d120b3b0--