Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:53782 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 58587 invoked from network); 8 Jul 2011 13:44:00 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Jul 2011 13:44:00 -0000 Authentication-Results: pb1.pair.com smtp.mail=ivan.enderlin@hoa-project.net; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=ivan.enderlin@hoa-project.net; sender-id=unknown Received-SPF: error (pb1.pair.com: domain hoa-project.net from 87.106.212.190 cause and error) X-PHP-List-Original-Sender: ivan.enderlin@hoa-project.net X-Host-Fingerprint: 87.106.212.190 s15355703.onlinehome-server.info Linux 2.6 Received: from [87.106.212.190] ([87.106.212.190:55896] helo=s15355703.onlinehome-server.info) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 1E/71-48961-D99071E4 for ; Fri, 08 Jul 2011 09:43:59 -0400 Received: from hwhost.local (85-218-19-237.dclient.lsne.ch [85.218.19.237]) by s15355703.onlinehome-server.info (Postfix) with ESMTPA id 1C15AE4138; Fri, 8 Jul 2011 15:43:54 +0200 (CEST) Message-ID: <4E170999.7040701@hoa-project.net> Date: Fri, 08 Jul 2011 15:43:53 +0200 Reply-To: ivan.enderlin@hoa-project.net User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:5.0) Gecko/20110624 Thunderbird/5.0 MIME-Version: 1.0 To: Rafael Dohms CC: Nikita Popov , PHP internals References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] preg_match From: ivan.enderlin@hoa-project.net ("Ivan Enderlin @ Hoa") Hi Rafael :-), On 08/07/11 15:18, Rafael Dohms wrote: > On Fri, Jul 8, 2011 at 9:20 AM, Nikita Popov wrote: >> The most common use for preg_match is validation: >> >> if (!preg_match('~...~', $string)) { /* do something */ } >> >> Here $matches is not required, only the 0/1 return value of preg_match is of >> interest. >> >> Furthermore, even if you need $matches, you should always combine it with an >> if: >> >> if (!preg_match('~...~', $string, $matches)) { /* do something with $matches >> */ } >> >> Otherwise you will access $matches even though the match failed (which will >> result in errors). >> >> Thus: There is no need to change behavior here. > That is just one use case as i see it its very cluncky to use this in > case you want to extract matches, you need to initilize the $matches > variable beforehad or you get a notice, so a simple extract is now 3 > lines of code. > > Thus, that is the reason why i suggest a new preg_extract function > that would handle the other use cases in a optimized way. Here is my proposition: function preg_extract ( $pattern, $subject ) { return false !== preg_match($pattern, $subject, $matches) ? $matches : false; } I think it is the trick you are looking for. But making a dedicated C implementation is not necessary IMHO. Best regards. -- Ivan Enderlin Developer of Hoa Framework http://hoa.42/ or http://hoa-project.net/ Member of HTML and WebApps Working Group of W3C http://w3.org/