This is a discussion on finding pattern with preg_match_all within the PHP Language forums, part of the PHP Programming Forums category; Determining the pattern below has got my stumped. I have a page of HTML and need to find all occurrences ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
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" <john+usenet@johndunlop.info> wrote in message news:MPG.19e8885e1eb54b91989774@news.freeserve.net ... > 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 |
|
|||
|
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. |
|
|||
|
"Pedro" <hexkid@hotpop.com> wrote in message news:blmn00$dt4kq$1@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 |