I was wondering if anyone ever thought of either fixing or writing a
new function that would make preg_match actually work in a way that
made sense?
right now i need to pass in a optional parameter that will receive the
match, in this case one or no match, why should this not be the
function's return already?
something like:
$string = "<td>my text</td>";
$result = preg_match("/<td>(.*)</td>/", $string);
$result // = "my text"
Maybe something like preg_extract? I do not have the C skills to write
a patch but i think adding a new function would not break BC or have
negative side effects, would it?
--
Rafael Dohms
PHP Evangelist and Community Leader
http://www.rafaeldohms.com.br
http://www.phpsp.org.br
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.
On Fri, Jul 8, 2011 at 2:12 PM, Rafael Dohms listas@rafaeldohms.com.brwrote:
I was wondering if anyone ever thought of either fixing or writing a
new function that would make preg_match actually work in a way that
made sense?right now i need to pass in a optional parameter that will receive the
match, in this case one or no match, why should this not be the
function's return already?something like:
$string = "<td>my text</td>";
$result = preg_match("/<td>(.*)</td>/", $string);$result // = "my text"
Maybe something like preg_extract? I do not have the C skills to write
a patch but i think adding a new function would not break BC or have
negative side effects, would it?--
Rafael Dohms
PHP Evangelist and Community Leader
http://www.rafaeldohms.com.br
http://www.phpsp.org.br
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
No, you don't need to initialize $matches. It's passed by reference and thus
doesn't need to be initialized.
And as I already said: It is good practice to ensure that preg_match
actually matched something:
if (preg_match(REGEX, $string, $matches)) {
// in here $matches is guaranteed to be defined
}
If you don't do that you will work with $matches even though there was no
match - which is pointless and
will probably result in bugs.
On Fri, Jul 8, 2011 at 3:18 PM, Rafael Dohms listas@rafaeldohms.com.brwrote:
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
Hi Rafael :-),
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/
Still, this is preg_match it only returns one match, why should i get
a array and have to use ugly things like $matches[0] afterwards?
It just makes for very ugly syntax and extra code, a simple function
would make this cleaner and more intuitive, first time using
preg_match is a nightmare.
--
Rafael Dohms
PHP Evangelist and Community Leader
http://www.rafaeldohms.com.br
http://www.phpsp.org.br
2011/7/8 Rafael Dohms listas@rafaeldohms.com.br:
Still, this is preg_match it only returns one match, why should i get
a array and have to use ugly things like $matches[0] afterwards?
It just makes for very ugly syntax and extra code, a simple function
would make this cleaner and more intuitive, first time using
preg_match is a nightmare.
You forget about subpatterns, so array can contain more than 1 value:
php > preg_match('/p(hp)/', 'php', $matches); var_dump($matches);
array(2) {
[0]=>
string(3) "php"
[1]=>
string(2) "hp"
}
--
Rafael Dohms
PHP Evangelist and Community Leader
http://www.rafaeldohms.com.br
http://www.phpsp.org.br--
--
Regards,
Shein Alexey
On July-08-11 10:01 AM Rafael Dohms wrote:
[snip]
first time using preg_match is a nightmare.
IMHO, preg_match is poetry in motion.
Going through a million lines of code replacing ereg[i] with preg_match
because it was deprecated in 5.3 - that is a nightmare.
Best Regards
Mike Robinson
On July-08-11 10:01 AM Rafael Dohms wrote:
[snip]
first time using preg_match is a nightmare.
IMHO, preg_match is poetry in motion.
Going through a million lines of code replacing ereg[i] with preg_match
because it was deprecated in 5.3 - that is a nightmare.Best Regards
Mike Robinson
--
Could've used preg_replace_all() ? Maybe? You are a programmer, right?
--
Richard Quadling
Twitter : EE : Zend : PHPDoc
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea
On July-08-11 10:01 AM Rafael Dohms wrote:
[snip]
first time using preg_match is a nightmare.
IMHO, preg_match is poetry in motion.
Going through a million lines of code replacing ereg[i] with preg_match
because it was deprecated in 5.3 - that is a nightmare.Best Regards
Mike Robinson
--
Could've used preg_replace_all() ? Maybe? You are a programmer, right?
that heading into the wrong direction.
to reply to the original thread: I also had some use cases, when
returning the match(es) would have been nice, and could have saved me
from typing some boilerplate code.
Tyrael