By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,640 Members | 1,598 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,640 IT Pros & Developers. It's quick & easy.

preg_match VS preg_match_all

P: n/a
Han
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.
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.
Jul 17 '05 #2

P: n/a
Han
Ah, dang it. I was so close, but got hung up on preg_match_all syntax.

Thanks for the clarification.

"Pedro" <he****@hotpop.com> wrote in message
news:bl************@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.

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.