Re: preg_match
| From: | Ivan Enderlin @ Hoa | Date: | Fri, 08 Jul 2011 13:43:53 +0000 |
| Subject: | Re: preg_match | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-53782@lists.php.net to get a copy of this message | ||
Hi Rafael :-),
On 08/07/11 15:18, Rafael Dohms wrote:
On Fri, Jul 8, 2011 at 9:20 AM, Nikita Popov<nikita.ppv@googlemail.com> wrote:function preg_extract ( $pattern, $subject ) {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:
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/