Extracting Data from Table

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Hyarion
Forum Newbie
Posts: 12
Joined: Tue Nov 01, 2005 4:30 am
Location: South Africa

Extracting Data from Table

Post by Hyarion »

I've got a few ADSL accounts that I want to keep track of easily. My ISP has a secure site where one can look up stats for each account for the month. I've made a script that logs into the site and retrieves the page with the stats and then cuts off the unnecessary bits before and after the stats (e.g. menu bar, disclaimer, etc.). It does this for as many accounts as I put into the script and then displays a summary of each account by displaying the table that has been extracted from the page.

I would like to extract the values out of that table and display them in my own way (or even save them to a db) but I have not worked with regex before. Could someone give me either a piece of code that could extract what I need or some good pointers please?

Here's the html code that's left after I've stripped the unnecessary bits (and is currently what just gets pasted into my summary page):

Code: Select all

<center><h3>Cumulative Totals for November 2005</h3></center>
<table cellspacing="1"><th>Sessions</th><th>Online Time (Seconds)</th><th>Upload (Bytes)</th><th>Download (Bytes)</th><th>Combined (Bytes)</th>
<tr><td>46</td><td>2934200</td><td>1 015 740 629</td><td>5 538 484 386</td><td>6 554 225 015</td></tr></table>
I need the Download, Upload and Combined values - I'm also guessing I'll need to strip the spaces from the values so it's treated as a number and not as a string.
yum-jelly
Forum Commoner
Posts: 98
Joined: Sat Oct 29, 2005 9:16 pm

Post by yum-jelly »

If you need to add numbers together then do it in a loop after extracting the data you want!

My notebook doesn't have PHP installed to test my example, but the example should work!

Code: Select all

<?

$parts = '<center><h3>Cumulative Totals for November 2005</h3></center> 
<table cellspacing="1"><th>Sessions</th><th>Online Time (Seconds)</th><th>Upload (Bytes)</th><th>Download (Bytes)</th><th>Combined (Bytes)</th> 
<tr><td>46</td><td>2934200</td><td>1 015 740 629</td><td>5 538 484 386</td><td>6 554 225 015</td></tr></table>';

$data = array ();

$regex = '#<th?d?>(.*)</th?d?>#Uis';

preg_match_all ( $regex, $parts, $out );

$max = ( sizeof ( $out[1] ) / 2 );
$len = $max;

for ( $i = 0; $i < $max; $i++ )
{
	$data[$i][] = trim ( $out[1][$i] );
	$numbers = explode ( ' ', trim ( $out[1][$len] ) );

	$count = 0;

	for ( $j = 0; $j < sizeof ( $numbers ); $j++ )
	{
		$number = trim ( $numbers[$j] );

		if ( ! empty ( $number ) )
		{
			$count += intval ( $number );
		}
	}

	$data[$i][] = $count;
	$len += 1;
}

print_r ( $data );

?>

yj
Hyarion
Forum Newbie
Posts: 12
Joined: Tue Nov 01, 2005 4:30 am
Location: South Africa

Post by Hyarion »

Thanks, that's what I was looking for. :)
Post Reply