Web Analytics Made Easy -
StatCounter preg_match help with parsing html - CodingForum

Announcement

Collapse
No announcement yet.

preg_match help with parsing html

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • preg_match help with parsing html

    I'm trying to incorporate some data into an intranet web page. The data I need is on this web page. It's a really simple design, but the html is pretty bad.

    I was thinking of using a regular expression to grab all the cell data, and put it into an 2 dimensional array. Something where $array[0] would be the first row and $array[1] would be the second row and so on.

    so
    PHP Code:
    $array[0][0]; 
    would be the first cell in the first row.

    I can't seem to get the regex to work any ideas?

    Here's what I'm using:

    PHP Code:
    $data[] = preg_replace("=\<td.*\>(.*?)\</td\>=is"'<strong>$1</strong>'$page); 
    Where $page is the html of the page I'm using.
    Create accessible online surveys -::- Koobten.com - compare netbook prices and reviews -::- Affordable, reliable hosting for less than $20 per year
    Zend Certified Engineer


  • #2
    you want to grab data but you're using preg_replace. What you want to do is use preg_match_all and look at the <tr> tags instead, that way you grab everything row by row.

    Here's a rough idea of what you would want, I didn't really test it but you can go from there.

    PHP Code:
    <?
    $start 
    "table borderColor=\"#c0c0c0\"";
    $end "collapse\" bordercolor=\"#111111\"";
    $url 'http://www.tsp.gov/rates/monthly-current.html';

    $handle fopen($url"r");
                    while((!(
    feof($handle))))
            {
                      
    $img $img.fread($handle1024);
            }
    //end while
    fclose ($handle);
    eregi("$start(.*)$end"$img$data); //go through specified part of the page

    preg_match_all("/<tr.*?>(.*?)<\/tr?>/is"
                
    $data[1], 
                
    $dat);

    echo 
    count($dat);

    // you will want to look at the $dat[1] array
    echo $dat[1][3];


    ?>
    Ithium | FootyMania.com

    Comment

    Working...
    X