finding pattern with preg_match_all

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-04-2003
Han
 
Posts: n/a
Default finding pattern with preg_match_all

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.


Reply With Quote
  #2 (permalink)  
Old 10-04-2003
John Dunlop
 
Posts: n/a
Default Re: finding pattern with preg_match_all

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
Reply With Quote
  #3 (permalink)  
Old 10-04-2003
Han
 
Posts: n/a
Default Re: finding pattern with preg_match_all

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})&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



Reply With Quote
  #4 (permalink)  
Old 10-04-2003
Pedro
 
Posts: n/a
Default Re: finding pattern with preg_match_all

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.
Reply With Quote
  #5 (permalink)  
Old 10-04-2003
Paulus Magnus
 
Posts: n/a
Default Re: finding pattern with preg_match_all


"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


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:39 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0