[preg_match_all] Better algorithm?

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

    [preg_match_all] Better algorithm?

    Hello

    I need to extract some bits of information from a web page, so
    I used preg_match_all. I was wondering if there's a simpler algorithm,
    or if that's the way it's done:

    ===========
    preg_match_all( "|$regex|smi",$ buffer,$matches ,PREG_PATTERN_O RDER);

    $elements=sizeo f($matches);
    $x=sizeof($matc hes,TRUE);
    $rows = $x/$elements;
    $rows--; //Why is the last row empty?

    print "elements=$elem ents, x=$x, $rows<br>";

    for ($row=0;$row < $rows;$row++){
    $string = "";
    for($element=1; $element < $elements;$elem ent++){
    $string .= $matches[$element][$row] . ",";
    }
    //Remove trailing ","
    $string = trim($string, ",");
    print "$string<hr >";
    }
    ===========

    Thank you.
  • gosha bine

    #2
    Re: [preg_match_all] Better algorithm?

    On 12.06.2007 20:37 Gilles Ganault wrote:
    Hello
    >
    I need to extract some bits of information from a web page, so
    I used preg_match_all. I was wondering if there's a simpler algorithm,
    or if that's the way it's done:
    >
    ===========
    preg_match_all( "|$regex|smi",$ buffer,$matches ,PREG_PATTERN_O RDER);
    >
    $elements=sizeo f($matches);
    $x=sizeof($matc hes,TRUE);
    $rows = $x/$elements;
    $rows--; //Why is the last row empty?
    >
    print "elements=$elem ents, x=$x, $rows<br>";
    >
    for ($row=0;$row < $rows;$row++){
    $string = "";
    for($element=1; $element < $elements;$elem ent++){
    $string .= $matches[$element][$row] . ",";
    }
    //Remove trailing ","
    $string = trim($string, ",");
    print "$string<hr >";
    }
    ===========
    >
    Thank you.
    Try using foreach to iterate through your arrays.

    --
    gosha bine

    extended php parser ~ http://code.google.com/p/pihipi
    blok ~ http://www.tagarga.com/blok

    Comment

    • Gilles Ganault

      #3
      Re: [preg_match_all] Better algorithm?

      On Wed, 13 Jun 2007 10:45:48 +0200, gosha bine <stereofrog@gma il.com>
      wrote:
      >Try using foreach to iterate through your arrays.
      I'll try it. Thanks.

      Comment

      Working...