Re: Re: [PROPOSAL]Add second to callback of preg_replace_callback

From: Date: Sat, 04 May 2013 12:52:04 +0000
Subject: Re: Re: [PROPOSAL]Add second to callback of preg_replace_callback
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-67293@lists.php.net to get a copy of this message
On Sat, May 4, 2013 at 8:49 PM, Nikita Popov <nikita.ppv@gmail.com> wrote: > On Sat, May 4, 2013 at 2:39 PM, Laruence <laruence@php.net> 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=latestc¼°y7´�Æ >> gS.w >> >> 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/

« previous php.internals (#67293) next »