Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:67296 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 78297 invoked from network); 4 May 2013 13:01:28 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 May 2013 13:01:28 -0000 Authentication-Results: pb1.pair.com header.from=laruence@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=laruence@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.217.181 as permitted sender) X-PHP-List-Original-Sender: laruence@gmail.com X-Host-Fingerprint: 209.85.217.181 mail-lb0-f181.google.com Received: from [209.85.217.181] ([209.85.217.181:51133] helo=mail-lb0-f181.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 9F/AB-26044-7A605815 for ; Sat, 04 May 2013 09:01:27 -0400 Received: by mail-lb0-f181.google.com with SMTP id w10so2261029lbi.40 for ; Sat, 04 May 2013 06:01:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type; bh=HmEMrTAcQGkZSqVNfYuA04IGIVR/szBM/5XZZ2SvD3U=; b=JdwcTjFPTQJhFabRvDSW9hoMw+HjRIS+60QGoKsLL6NP9TvKEVGqf01FLIp2P9bfL4 QgCDwepjIIZGeFyczmyuAPkBUjo+zlC5nQcRDHen9K8fKQNcVJk9CLTlzOxJKpB4RUei zM+S/Rm6gn/2VQ+CGbty+VMmT05XU9RUg97HFrKw0tC6og3ic4U+klooTrh2zS5jFoNT V3tBXRKYCBJ5OW6aGN7th8nse02BeLCR2WWhafMwrEZAJT64h98wUmNW9XI//QQ4bNOa gyeRwOnMp135Hbts5jqWmVpJb0sHZZ+EQ3YFenI2dkMFryElE1t3jgWem8FCWvLz3tan Yahg== X-Received: by 10.152.170.199 with SMTP id ao7mr4352359lac.51.1367672484602; Sat, 04 May 2013 06:01:24 -0700 (PDT) MIME-Version: 1.0 Sender: laruence@gmail.com Received: by 10.114.11.129 with HTTP; Sat, 4 May 2013 06:01:04 -0700 (PDT) In-Reply-To: References: Date: Sat, 4 May 2013 21:01:04 +0800 X-Google-Sender-Auth: kj2pDrp0H5Nsu9lEWP1SOwiz7D0 Message-ID: To: Nikita Popov Cc: PHP Internals Content-Type: multipart/alternative; boundary=089e01176b299d291a04dbe410f9 Subject: Re: [PHP-DEV] Re: [PROPOSAL]Add second to callback of preg_replace_callback From: laruence@php.net (Laruence) --089e01176b299d291a04dbe410f9 Content-Type: text/plain; charset=UTF-8 On Sat, May 4, 2013 at 8:58 PM, Nikita Popov wrote: > 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); > } > So if there are 10 regex in the array, you will got 10 DO_FCALL. 10 times arguments accept, 10 times etc... and AS I already said: IT is inefficient. I personally can not accept it. thanks > > Which is both clearer and requires less code. > > Nikita > -- Laruence Xinchen Hui http://www.laruence.com/ --089e01176b299d291a04dbe410f9--