Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:67294 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 75530 invoked from network); 4 May 2013 12:58:41 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 May 2013 12:58:41 -0000 Authentication-Results: pb1.pair.com smtp.mail=laruence@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=laruence@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.51 as permitted sender) X-PHP-List-Original-Sender: laruence@gmail.com X-Host-Fingerprint: 209.85.215.51 mail-la0-f51.google.com Received: from [209.85.215.51] ([209.85.215.51:50022] helo=mail-la0-f51.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 7B/0B-26044-EF505815 for ; Sat, 04 May 2013 08:58:39 -0400 Received: by mail-la0-f51.google.com with SMTP id ep20so2177780lab.24 for ; Sat, 04 May 2013 05:58:36 -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=8WGRYDRiadsR7FMvWgzTg37JHFE/mOjyl23mq3lEf78=; b=PmikSdSQv9XNWh/GiMGLoB6zNXTfDULvqip1VJzd5WJ/TKgBxVvs9M7whfaGjGpZfQ oSyDukx64clUfnkoXlI9PpMSs16YE7QGttQd39MRNyuAITOIyiDyP1QwtWH7nCIiUSjf 1Z3ALxU1MVPB7Yl60HhZx8/x7bckCO987Zua7Iy92NhCMoQLoINg8daJWPKvGMReKaVc +PPlBNMv3kiKC5MTsE9weaUYZ+kXMaE9/cL6MN4MVjClJwp/aeyKRIWtg2U2ui2+8Hkk uD8dDnc7CwqJbNKWMHjOVARN/Qx7Ce7+dczQsfYhxQlY80g4CxhZLWtRKlO8N85Yo5W+ d5yg== X-Received: by 10.112.158.164 with SMTP id wv4mr5431322lbb.115.1367672315953; Sat, 04 May 2013 05:58:35 -0700 (PDT) MIME-Version: 1.0 Sender: laruence@gmail.com Received: by 10.114.11.129 with HTTP; Sat, 4 May 2013 05:58:15 -0700 (PDT) In-Reply-To: References: Date: Sat, 4 May 2013 20:58:15 +0800 X-Google-Sender-Auth: w1elDydKANhn62mj4otgzmadWIw Message-ID: To: Nikita Popov Cc: PHP Internals Content-Type: multipart/alternative; boundary=001a11c3896690518004dbe40666 Subject: Re: [PHP-DEV] Re: [PROPOSAL]Add second to callback of preg_replace_callback From: laruence@php.net (Laruence) --001a11c3896690518004dbe40666 Content-Type: text/plain; charset=UTF-8 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. > And, I *DO NOT* think it's weird, it's few choice we got now.. because: As I said, a loop is inefficient here. > > One thing that might be nicer API wise but without ambiguity is an > preg_replace_callback(['regex' => callback], $string) overload. Though > personally I'm not really a fan of doing overloads with completely > different argument types. > I don't this this is possible.. thinking of : $regex = array(0 => "xxx"); if by accident there is a function call "xxx". thanks > > > Nikita > -- Laruence Xinchen Hui http://www.laruence.com/ --001a11c3896690518004dbe40666--