Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 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.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Depends on the time of year
    Posts
    478
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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];


    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •