473,419 Members | 4,382 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,419 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 3781
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.