473,225 Members | 1,328 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,225 software developers and data experts.

finding pattern with preg_match_all

Han
Determining the pattern below has got my stumped.

I have a page of HTML and need to find all occurrences of the following
pattern:

score=9999999999&

The number shown can be 5-10 characters in length. I would like to extract
only the number, stripping off the "score=" and "&".

I have tried the following without success:

preg_match_all("|score=(\d{5,10})&|is", $html, $out);

The page includes numerous occurrences of the pattern yet the $out array
only contains two items with both values equaling "array".

Any help would be greatly appreciated.

Thanks.
Jul 17 '05 #1
4 3775
Han wrote:
preg_match_all("|score=(\d{5,10})&|is", $html, $out);

The page includes numerous occurrences of the pattern yet the $out
array only contains two items with both values equaling "array".


Well, your regex works. (Note that the s modifier in your pattern
is superfluous, as it only affects dot metacharacters.)

But $out is an array containing two arrays. That is, an array of
arrays. $out[0] is an array of full pattern matches; $out[1] is an
array of first parenthesized subpattern matches. To access just
the numbers, you might use:

foreach($out[1] as $number)
echo $number;

You can set flags in preg_match_all that change how the arrays are
filled.
http://www.php.net/manual/en/functio...-match-all.php

--
Jock
Jul 17 '05 #2
Han
Yes, that makes perfect sense.

I'm quite familiar with php.net (couldn't survive without in fact), but I
began to doubt my syntax when the right number of results didn't seem to
appear. My regexp was working all along, but I wasn't outputting it
correctly (dumb!)

Sigh. Thanks for the clarification.

"John Dunlop" <jo*********@johndunlop.info> wrote in message
news:MP************************@news.freeserve.net ...
Han wrote:
preg_match_all("|score=(\d{5,10})&amp;|is", $html, $out);

The page includes numerous occurrences of the pattern yet the $out
array only contains two items with both values equaling "array".


Well, your regex works. (Note that the s modifier in your pattern
is superfluous, as it only affects dot metacharacters.)

But $out is an array containing two arrays. That is, an array of
arrays. $out[0] is an array of full pattern matches; $out[1] is an
array of first parenthesized subpattern matches. To access just
the numbers, you might use:

foreach($out[1] as $number)
echo $number;

You can set flags in preg_match_all that change how the arrays are
filled.
http://www.php.net/manual/en/functio...-match-all.php

--
Jock

Jul 17 '05 #3
Han wrote:
[...]
My regexp was working all along, but I wasn't outputting it
correctly (dumb!)


When debugging arrays I find the construct

<?php
echo '<pre>'; print_r($array); echo '</pre>';
?>

very enlightning.
--
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 #4

"Pedro" <he****@hotpop.com> wrote in message
news:bl************@ID-203069.news.uni-berlin.de...
Han wrote:
[...]
My regexp was working all along, but I wasn't outputting it
correctly (dumb!)


When debugging arrays I find the construct

<?php
echo '<pre>'; print_r($array); echo '</pre>';
?>

very enlightning.


I agree but I also use the fancy_vardump function from the user contributed
notes on php.net for var_dump. It's a lot better at displaying the contents
of arrays. If I get really bogged down I open up phpEd and step-thru the
code and examine the arrays as they're being used.

Paulus
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: petrovitch | last post by:
While using the following loop to extract images from the google search engine I discovered that preg_match_all works much faster parsing small strings in a loop than extracting all of the urls at...
1
by: Tristan | last post by:
Im trying to expand a search util by uing regular expression to allow common search criteria such as +-* and phrases "". My understanding of ereg(string pattern, string string, ) is that the...
4
by: Fabian | last post by:
Hi all there, Sorry for this newbee question but how comes that the following pattern: $r = "%<td valign=top><a href=\"(+?)\"(.*?)>%"; does not return any result, while the pattern: $r = "%<a...
4
by: Fabian | last post by:
Hi all there, I have already tried asking for help a couple of days ago. I try to rephrase better my problem: I need to grab a webpage that looks like this: <td width=80 align=center...
13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
5
by: Damien | last post by:
Hi to all, I would like to get field values from a multi-line text. For instance : " Name: Mr Smith Address: Paris " I've tried to use (I'm a total regexp newbie, so I took it from the...
3
by: David Moore | last post by:
Hi All, I expect someone can crack this one in no time. Ok, what I'd like to do is to match a pattern in a string and extract a portion of it. For example if I had a string like: 'The store...
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...
2
loriann
by: loriann | last post by:
hi, I have a problem with preg_match function returning empty arrays for my wonderful regexes. However, I can't see what I am doing wrong - maybe one of you could help? I'm loading the source...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.