preg_match VS preg_match_all

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&...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-04-2003
Han
 
Posts: n/a
Default preg_match VS preg_match_all

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.


Reply With Quote
  #2 (permalink)  
Old 10-04-2003
Pedro
 
Posts: n/a
Default Re: preg_match VS preg_match_all

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.
Reply With Quote
  #3 (permalink)  
Old 10-04-2003
Han
 
Posts: n/a
Default Re: preg_match VS preg_match_all

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&amp;"
> >
> > preg_match_all ("|product=(\d{5,10})&amp;|i", $html, $out);
> >
> > $out[1][0] = 3456789
> >
> > preg_match ("|product=(\d{5,10})&amp;|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.



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:44 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0