This is a discussion on preg_match VS preg_match_all within the PHP Language forums, part of the PHP Programming Forums category; I'm wondering if someone can explain why the following works with preg_match_all, but not preg_match: $html = "product=3456789&...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I'm wondering if someone can explain why the following works with
preg_match_all, but not preg_match: $html = "product=3456789&" preg_match_all ("|product=(\d{5,10})&|i", $html, $out); $out[1][0] = 3456789 preg_match ("|product=(\d{5,10})&|i", $html, $out); $out[1][0] = 3 For some reason, preg_match only returns the first character of the match. Is this by design or does the regexp pattern need to be modified? I'm curious to know what the difference is. Thanks in advance. |
|
|||
|
Han wrote:
> I'm wondering if someone can explain why the following works with > preg_match_all, but not preg_match: > > $html = "product=3456789&" > > preg_match_all ("|product=(\d{5,10})&|i", $html, $out); > > $out[1][0] = 3456789 > > preg_match ("|product=(\d{5,10})&|i", $html, $out); > > $out[1][0] = 3 > > For some reason, preg_match only returns the first character of the match. > Is this by design or does the regexp pattern need to be modified? > > I'm curious to know what the difference is. It is by design! preg_match() returns the first and *ONLY* the first match. preg_match_all() returns an array with *ALL* the matches. after your preg_match() $out[1] = 3456789 $out[1][0] = 3 $out[1][1] = 4 $out[1][2] = 5 ... the second index of $out[1] represents the character index inside the string, $out[1] is the first match of the first set of ( ) -- I have a spam filter working. To mail me include "urkxvq" (with or without the quotes) in the subject line, or your mail will be ruthlessly discarded. |
|
|||
|
Ah, dang it. I was so close, but got hung up on preg_match_all syntax.
Thanks for the clarification. "Pedro" <hexkid@hotpop.com> wrote in message news:blnape$e05os$1@ID-203069.news.uni-berlin.de... > Han wrote: > > I'm wondering if someone can explain why the following works with > > preg_match_all, but not preg_match: > > > > $html = "product=3456789&" > > > > preg_match_all ("|product=(\d{5,10})&|i", $html, $out); > > > > $out[1][0] = 3456789 > > > > preg_match ("|product=(\d{5,10})&|i", $html, $out); > > > > $out[1][0] = 3 > > > > For some reason, preg_match only returns the first character of the match. > > Is this by design or does the regexp pattern need to be modified? > > > > I'm curious to know what the difference is. > > It is by design! > > preg_match() returns the first and *ONLY* the first match. > preg_match_all() returns an array with *ALL* the matches. > > after your preg_match() > $out[1] = 3456789 > $out[1][0] = 3 > $out[1][1] = 4 > $out[1][2] = 5 > ... > the second index of $out[1] represents the character index inside the > string, $out[1] is the first match of the first set of ( ) > > -- > I have a spam filter working. > To mail me include "urkxvq" (with or without the quotes) > in the subject line, or your mail will be ruthlessly discarded. |