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

finding pattern with preg_match_all

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.