Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:67295 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 76106 invoked from network); 4 May 2013 12:59:01 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 May 2013 12:59:01 -0000 Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.219.48 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.219.48 mail-oa0-f48.google.com Received: from [209.85.219.48] ([209.85.219.48:43764] helo=mail-oa0-f48.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 97/2B-26044-41605815 for ; Sat, 04 May 2013 08:59:01 -0400 Received: by mail-oa0-f48.google.com with SMTP id i4so54810oah.35 for ; Sat, 04 May 2013 05:58:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=pJSXXIrGZbnkn3Z/iCmGrOiJfoWlIctkzeXL+a7N1jE=; b=m+EB2GwFL+twxpC6USw7l5uAmbBPnE3VCOzxpgjQlJup8p3WhSFeEmVFV51RcA6eao 8qqJmxGQ5NWeYf26aIYTm4iORi3Q1N1b92VACSjN2iychTFJmboLAS7e+hAkt+zxWB3X 59XAa09kwLAVAGRpZ8GPTP6aXPKagt7fQtwfnG/Db6GhDgmy1X99/W0e5sqVaYzJibKk Y+DOehZaiHyYLqZbPNBlCGUt6SwoS++kdTt/78C/oaO0BbM2S1qFV/ZPOvwAcW2LBqcI tSdesWPUbHMQdBYqXFnGUaNK4YCLKxpL/ZJA9UDN7MrUaMgggpjaKXfzepZeWsWmOSUP Kjug== MIME-Version: 1.0 X-Received: by 10.182.105.227 with SMTP id gp3mr3820906obb.81.1367672338338; Sat, 04 May 2013 05:58:58 -0700 (PDT) Received: by 10.182.49.136 with HTTP; Sat, 4 May 2013 05:58:58 -0700 (PDT) In-Reply-To: References: Date: Sat, 4 May 2013 14:58:58 +0200 Message-ID: To: Laruence Cc: PHP Internals Content-Type: multipart/alternative; boundary=089e013a0510e55a3804dbe407bd Subject: Re: [PHP-DEV] Re: [PROPOSAL]Add second to callback of preg_replace_callback From: nikita.ppv@gmail.com (Nikita Popov) --089e013a0510e55a3804dbe407bd Content-Type: text/plain; charset=ISO-8859-1 On Sat, May 4, 2013 at 2:52 PM, Laruence wrote: > > > > On Sat, May 4, 2013 at 8:49 PM, Nikita Popov wrote: > >> On Sat, May 4, 2013 at 2:39 PM, Laruence wrote: >> >>> Hey: >>> Sorry for the delay, the new patch, which make the second argument >>> the >>> regex array keys is attached. >>> >>> >>> >>> https://bugs.php.net/patch-display.php?bug_id=64730&patch=second_arg_rege_key.patch&revision=latest >>> >>> with this patch, preg_replace_callback will call user callback with >>> two >>> arguments, the first one is the same, the second is the regex key if the >>> regex is an array or NULL if the regex is a string. >>> >>> then we can do something like: >>> >>> $code = preg_replace_callback( >>> array( >>> "foo" => "/some very complex regex/", >>> "bar" => "/another one/", >>> ... >>> ), >>> function($matches, $idx) { >>> switch ($idx) { >>> case 'foo' >>> ... >>> case 'bar': >>> ... >>> } >>> }, >>> $code); >>> >>> >>> if no objections, I will commit this patch after 5.5. 0 final >>> release.. >>> >>> thanks >>> >> >> I object. If this were using the preg_replace_callback(Array, Array, >> String) syntax [which is not practically possible due to ambiguity], I >> would agree that this is a nice feature which makes the function consistent >> with preg_replace and str_replace. But this implementation with some weird >> additional identifier that you need to switch over makes absolutely no >> sense to me and only complicated the API. Better to just use a loop for >> this. >> > if you got an better solution, please propose it. > > only object won't help much here, since the problem is there, we need to > solve it. > > thanks > Sorry, but what problem is there again? As I already said earlier, you can just use a loop: foreach ($replacements as $regex => $callback) { $str = preg_replace_callback($regex, $callback, $str); } Which is both clearer and requires less code. Nikita --089e013a0510e55a3804dbe407bd--