Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:67293 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 73846 invoked from network); 4 May 2013 12:52:29 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 May 2013 12:52:29 -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.217.178 as permitted sender) X-PHP-List-Original-Sender: laruence@gmail.com X-Host-Fingerprint: 209.85.217.178 mail-lb0-f178.google.com Received: from [209.85.217.178] ([209.85.217.178:64951] helo=mail-lb0-f178.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 9A/AA-26044-B8405815 for ; Sat, 04 May 2013 08:52:28 -0400 Received: by mail-lb0-f178.google.com with SMTP id x10so2229315lbi.23 for ; Sat, 04 May 2013 05:52: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=Br+3Q5jrPGFoKGNv3ys0pMYrnLFydY7nwF4ItYNbO10=; b=ZJ4FjUwMoF8cQdC/VE5YZ/ZEZl3bBvJ+uLkzqraWODw4+ujy+bby35WAn3KJSQLqYM bbk78WMoREq8izSeF8WIaAoU8VI8orRrudJu/jYx/rlLB1CtEKfOhNDNVpCgxzOyEJwL rATp3RKR/uqNUsYAoVJxTNotOqTLgnB0k7dyoWLFQv9pcYdjirhx1EU08LFHpIew4LHK H5qES5E6to94ORQeBm2PLLCz6YdFTOIU56+PdyTjyXE73eJ8uY/NGemVA2fsj9mIPMMc Sz/EKOll8MHYPW78e0h/dLvn9tHujDYagch0ESgvu6doCtpwU8lC4oJWoNir+zO2VO0s Qcdg== X-Received: by 10.112.3.202 with SMTP id e10mr5716038lbe.84.1367671944861; Sat, 04 May 2013 05:52:24 -0700 (PDT) MIME-Version: 1.0 Sender: laruence@gmail.com Received: by 10.114.11.129 with HTTP; Sat, 4 May 2013 05:52:04 -0700 (PDT) In-Reply-To: References: Date: Sat, 4 May 2013 20:52:04 +0800 X-Google-Sender-Auth: 0EKMo7_wNkkXkkJ_vkt6aQ8EJF4 Message-ID: To: Nikita Popov Cc: PHP Internals Content-Type: multipart/alternative; boundary=14dae9473a79715d9f04dbe3f0dd Subject: Re: [PHP-DEV] Re: [PROPOSAL]Add second to callback of preg_replace_callback From: laruence@php.net (Laruence) --14dae9473a79715d9f04dbe3f0dd 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. > 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 > > 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. > > Nikita > -- Laruence Xinchen Hui http://www.laruence.com/ --14dae9473a79715d9f04dbe3f0dd--