Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:67291 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 70573 invoked from network); 4 May 2013 12:39:36 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 May 2013 12:39:36 -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.176 as permitted sender) X-PHP-List-Original-Sender: laruence@gmail.com X-Host-Fingerprint: 209.85.217.176 mail-lb0-f176.google.com Received: from [209.85.217.176] ([209.85.217.176:51287] helo=mail-lb0-f176.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 42/F9-26044-58105815 for ; Sat, 04 May 2013 08:39:34 -0400 Received: by mail-lb0-f176.google.com with SMTP id v20so2224143lbc.7 for ; Sat, 04 May 2013 05:39:30 -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:content-type; bh=lUYSB9zsjPDnn3Yb4wFu9+6K09sj/dpGB8JTydCQNKw=; b=EosMdkTvel5Ie5QMWp6/yrWuBjNoOvnNOmSdBThdboz1YLnmOeAFvPqo/E+xrfIryW rQw3R9DAB1EjiHJK3DUgrtDJtKavzwC58aqtxSuJizMcRXXZ8nxlWwccVWq1tnyQlIV+ RmmXQoKKEg0nTM0SnRBq8jpg+zShUQxzlQ8XFTTLbbIhfjXJlbq6G0XnLG88+Ei/NHQR gNBgN4PI56SfhbDcZTXy0H2BJZLWEUKIA4jUZBPG+PDLCvwL9/j1SSZIQl89ar95azl5 yB0CnP+xo3auJ4Omk3zqYBzKhg/X6QphFo/B3h2EMd/vzCjZZdBbAowLUNjsVJR15m4w 6Y5g== X-Received: by 10.112.133.200 with SMTP id pe8mr5535692lbb.50.1367671170703; Sat, 04 May 2013 05:39:30 -0700 (PDT) MIME-Version: 1.0 Sender: laruence@gmail.com Received: by 10.114.11.129 with HTTP; Sat, 4 May 2013 05:39:10 -0700 (PDT) In-Reply-To: References: Date: Sat, 4 May 2013 20:39:10 +0800 X-Google-Sender-Auth: KS7dpEfez371ZpBAYJ1XyTYmtL8 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary=047d7b3433604ca70104dbe3c2c4 Subject: Re: [PROPOSAL]Add second to callback of preg_replace_callback From: laruence@php.net (Laruence) --047d7b3433604ca70104dbe3c2c4 Content-Type: text/plain; charset=UTF-8 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 On Tue, Apr 30, 2013 at 12:46 AM, Laruence wrote: > Hey: > there comes a FR: https://bugs.php.net/bug.php?id=64730 > > the main idea is, in 5.5 we remove support of 'e' modifier. > > then comes a problem, the old codes(a real use case see > https://github.com/php/php-src/blob/PHP-5.4/Zend/zend_vm_gen.php#L390): > > preg_replace(array( > > "/pattern1(.*)/", > "/pattern2(.*)/" > ), > array( > "/replace1/e", > "/replace2/e" > ) > ..), > > can not be easier convert to the "callback" style. > > then I have to change it to something very ugly like(a real use case > see: https://github.com/php/php-src/blob/PHP-5.5/Zend/zend_vm_gen.php#L390 > ): > > function callback($subject) { > if (!strncmp($subject, "pattern1", 8)) { > //function for pattern 1 > } else if(!strncmp($subject, "pattern2", 8)) { > //function for pattern 2 > } else ..... > > } > > so I propose to add a second argument to callback(aim to php-5.5.1), > which is the matched regex string self. > > then I can simplify the previous codes to: > > function callback ($subject, $regex) { > $replace_funcs = array( > "/pattern1(.*)" => function ($subect) { //function for > parttern 1; }, > "/pattern2(.*)" => function($sbuect) { //function for > pattern 2; } > ); > > $replace_funcs[$regex]($subject); > } > > what do you think(of course, the second argument can also easily change > to be the regex idx in the regex array)? > > patch is here: > https://bugs.php.net/patch-display.php?bug_id=64730&patch=sencode_argument.patch&revision=latest > > -- > Laruence Xinchen Hui > http://www.laruence.com/ > -- Laruence Xinchen Hui http://www.laruence.com/ --047d7b3433604ca70104dbe3c2c4--