Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:67205 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 65060 invoked from network); 30 Apr 2013 02:31:41 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Apr 2013 02:31: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.217.172 as permitted sender) X-PHP-List-Original-Sender: laruence@gmail.com X-Host-Fingerprint: 209.85.217.172 mail-lb0-f172.google.com Received: from [209.85.217.172] ([209.85.217.172:36787] helo=mail-lb0-f172.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 47/A1-48933-B0D2F715 for ; Mon, 29 Apr 2013 22:31:40 -0400 Received: by mail-lb0-f172.google.com with SMTP id o10so106586lbi.17 for ; Mon, 29 Apr 2013 19:31: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=AVLpBVQl67UybTD3Qmj/uJ2TRnE74V/BECStxJcaR1o=; b=VdZJVxkI+WhJO8MOZnAB/XiUjjnqVYgMrG0WGNrrN7qdE8LStOQgE7NlUuFa/sLMQh fXwEV/xzh9SER/M3FMxjLgr+YUpxQiqwZy1Wt8rHoAAakc3gmnhJCV2GYYfYAzxW53V2 qrfhGhjB6yilkjkwSqxQvmtENPoAWiOTCR455De9juaf7dd4QmtfpJNjrkjh1k+uNYho rgRkzPmUVmNcTZ3Pa2AfU42a+oZ0pVIJWCg5coz7cdzWNil1oKoZDVjfc7jUeQVzClS8 Lz0REY4+3tx4TNysYVAPt7e+qYMNpKi9LvW2hXLm6wjKTt/eHTzoqO6I0K1ZdYX9/MI+ oY2w== X-Received: by 10.152.4.10 with SMTP id g10mr557944lag.41.1367289096586; Mon, 29 Apr 2013 19:31:36 -0700 (PDT) MIME-Version: 1.0 Sender: laruence@gmail.com Received: by 10.114.97.231 with HTTP; Mon, 29 Apr 2013 19:31:16 -0700 (PDT) In-Reply-To: References: Date: Tue, 30 Apr 2013 10:31:16 +0800 X-Google-Sender-Auth: 5MSV2317FPakwJj4_LJ8F4ZGFfw Message-ID: To: Nikita Popov Cc: PHP Internals Content-Type: multipart/alternative; boundary=089e013d1a42e849c904db8acc7c Subject: Re: [PHP-DEV] [PROPOSAL]Add second to callback of preg_replace_callback From: laruence@php.net (Laruence) --089e013d1a42e849c904db8acc7c Content-Type: text/plain; charset=UTF-8 On Tue, Apr 30, 2013 at 1:43 AM, Nikita Popov wrote: > On Mon, Apr 29, 2013 at 6:46 PM, 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 >> >> > What's wrong with this? > > $replacements = ['regex' => 'callback']; > foreach ($replacements as $regex => $callback) { > $str = preg_replace_callback($regex, $callback, $str); > hey: efficiency & simplicity do you use foreach even there is array_map? thanks > } > > Nikita > -- Laruence Xinchen Hui http://www.laruence.com/ --089e013d1a42e849c904db8acc7c--