# preg_match

- Thread: https://externals.io/message/53777
- Original archive: https://news-web.php.net/php.internals/53777
- Started by Rafael Dohms on 2011-07-08T12:12:37+00:00
- Messages: 10
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #53777 — Rafael Dohms — 2011-07-08T12:12:37+00:00

URL: https://externals.io/message/53777#53777

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

---

## #53778 — Nikita Popov — 2011-07-08T12:20:26+00:00

URL: https://externals.io/message/53777#53778 · In reply to: #53777

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.br>wrote:

> 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

---

## #53780 — Rafael Dohms — 2011-07-08T13:18:34+00:00

URL: https://externals.io/message/53777#53780 · In reply to: #53778

> 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

---

## #53781 — Nikita Popov — 2011-07-08T13:27:12+00:00

URL: https://externals.io/message/53777#53781 · In reply to: #53780

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.br>wrote:

> 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

---

## #53782 — Ivan Enderlin @ Hoa — 2011-07-08T13:43:53+00:00

URL: https://externals.io/message/53777#53782 · In reply to: #53780

Hi Rafael :-),


> 
> > […]
> 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/

---

## #53783 — Rafael Dohms — 2011-07-08T14:01:03+00:00

URL: https://externals.io/message/53777#53783 · In reply to: #53782

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

---

## #53784 — Alexey Shein — 2011-07-08T14:11:38+00:00

URL: https://externals.io/message/53777#53784 · In reply to: #53783

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

---

## #53786 — Mike Robinson — 2011-07-08T15:31:15+00:00

URL: https://externals.io/message/53777#53786 · In reply to: #53783

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

---

## #53787 — Richard Quadling — 2011-07-08T16:09:44+00:00

URL: https://externals.io/message/53777#53787 · In reply to: #53786

> On July-08-11 10:01 AM Rafael Dohms wrote:
>
> [snip]
>
> > […]
>
> 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

---

## #53790 — Ferenc Kovacs — 2011-07-08T16:18:55+00:00

URL: https://externals.io/message/53777#53790 · In reply to: #53787

> 
> > […]
>
> 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
