Re: preg_match
| From: | Rafael Dohms | Date: | Fri, 08 Jul 2011 13:18:34 +0000 |
| Subject: | Re: preg_match | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-53780@lists.php.net to get a copy of this message | ||
On Fri, Jul 8, 2011 at 9:20 AM, Nikita Popov <nikita.ppv@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.
--
Rafael Dohms
PHP Evangelist and Community Leader
http://www.rafaeldohms.com.br
http://www.phpsp.org.br