View Full Version : preg_match help with parsing html

03-08-2004, 06:07 PM
I'm trying to incorporate some data into an intranet web page. The data I need is on this (http://www.tsp.gov/rates/monthly-current.html) 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.



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:

$data[] = preg_replace("=\<td.*\>(.*?)\</td\>=is", '<strong>$1</strong>', $page);

Where $page is the html of the page I'm using.

03-08-2004, 10:52 PM
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.

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

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


echo count($dat);

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