473,320 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

preg_match VS preg_match_all

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
2 4665
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Han | last post by:
I know this is possible (because preg can do almost anything!), but can't get a handle on the syntax. I have an HTML string: <font size="3"><a...
2
by: Han | last post by:
The following pattern (which is one subpattern in a string of several) looks for the following $xxx,xxx.xx (with the dollar sign) or xxx,xxx.xx (space in replace of missing dollar sign) ...
22
by: Justin Koivisto | last post by:
OK, I found a thread that help out from a while back (Oct 9, 2002) to give me this pattern: `(((f|ht)tp://)((+)(+)?@)?()+(:\d+)?(\/+)?)`i OK, all is well and good with this until the URL is...
1
by: Jens Thiede | last post by:
Quick-Fix: Can preg_match_all return the indexes of where it matched the string? More Detail: Read carefully: I'd like to seperate a string's parts into two (2) arrays which can be subdivide...
2
by: Muumac | last post by:
I have problem with large textfiles! When I load over 4MB xml and then try to preg_match something in this I get always FALSE! I have <File>....</File> tags in XML. Between tags is files contents...
22
by: stoppal | last post by:
need to extract all text between the following strings, but not include the strings. "<!-- #BeginEditable "Title name" -->" "<p align="center">#### </p>" I am using preg_match(????, $s,...
10
by: greatprovider | last post by:
i'm starting with a string such as "Na**3C**6H**5O**7*2H**20" im attempting to match all **\d+ ...once i can match all the double asterix \d i intend to wrap the \d in "<sub>" tags for display...
6
by: MB | last post by:
I want to be able to extract key/value pairs from a string but I am not succeeding. Experimenting and googling for a few hours didn't get me anywhere so I'm hoping for help here. My input string...
11
by: nintesa | last post by:
Hello... I'm trying to write a regular expression to match the content of a html tag. I need to match the content of <h1i.e. <h1>Hello World</h1> <h1 class="red_background">Hello World</h1>...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.