finding pattern with preg_match_all

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Han

    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=999999999 9&

    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,1 0})&|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.


  • John Dunlop

    #2
    Re: finding pattern with preg_match_all

    Han wrote:
    [color=blue]
    > preg_match_all( "|score=(\d{5,1 0})&|is", $html, $out);
    >
    > The page includes numerous occurrences of the pattern yet the $out
    > array only contains two items with both values equaling "array".[/color]

    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.


    --
    Jock

    Comment

    • Han

      #3
      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@jo hndunlop.info> wrote in message
      news:MPG.19e888 5e1eb54b9198977 4@news.freeserv e.net...[color=blue]
      > Han wrote:
      >[color=green]
      > > preg_match_all( "|score=(\d{5,1 0})&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".[/color]
      >
      > 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[/color]


      Comment

      • Pedro

        #4
        Re: finding pattern with preg_match_all

        Han wrote:
        [...][color=blue]
        > My regexp was working all along, but I wasn't outputting it
        > correctly (dumb!)[/color]

        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.

        Comment

        • Paulus Magnus

          #5
          Re: finding pattern with preg_match_all


          "Pedro" <hexkid@hotpop. com> wrote in message
          news:blmn00$dt4 kq$1@ID-203069.news.uni-berlin.de...[color=blue]
          > Han wrote:
          > [...][color=green]
          > > My regexp was working all along, but I wasn't outputting it
          > > correctly (dumb!)[/color]
          >
          > When debugging arrays I find the construct
          >
          > <?php
          > echo '<pre>'; print_r($array) ; echo '</pre>';
          > ?>
          >
          > very enlightning.[/color]

          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


          Comment

          Working...