Hi Rafael :-),

On 08/07/11 15:18, Rafael Dohms wrote:
On Fri, Jul 8, 2011 at 9:20 AM, Nikita Popov<nikita....@googlemail.com>  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/


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to