Jump to content

[SOLVED] Retrieving info from a table on a web page, how can I do that?


John_S

Recommended Posts

Hello there everybody!

As the title says I am searching a way to get content from a table on an external webpage. I tried what I could but without any results.

I proceeded this way:First I get content from the page with:

<?php
$source = file_get_contents('URL');
?>

Then I isolate the table I need the info from:

<?phppreg_match_all('#<TABLE BORDER=0 CELLPADDING=4 CELLSPACING=1 WIDTH=100%>(.*?)</TABLE>#s', $source, $entries);

But when I arrive there, I don't know how to extract table rows and the info I need. The structure of a table row looks like

<TR BGCOLOR=" DEPENDS ON THE ROW "><TD WIDTH=10%>Info I need 1</TD><TD WIDTH=55%><A HREF=" AN URL">Info I need 2</A></TD><TD WIDTH=15%>Info I need 3</TD><TD WIDTH=20%>Info I need 4</TD></TR>

Does anybody know how I can get that info?

Thanks a lot in advance!Yours John_S

well preg_match_all() returns and array of all the matches.  So all you have to do is just loop through them.

 

foreach($entries AS $entry)
   echo $entry . "
"

?>

 

You just have to incorporate $entry into your table.  Hope this is what you're looking for.

strip_tags on the data...

 

<?php
$cols = array(); // initiate the array.

foreach($entries AS $entry)
    $cols[] = rim(strip_tags($entry)); // remove the html tags and trim for good measure.

echo "<pre>", print_r($cols), "</pre>";
?>

It just means display_errors is set to off. I had an error in the code.

 

<?php
$cols = array(); // initiate the array.

foreach($entries AS $entry)
    $cols[] = trim(strip_tags($entry)); // remove the html tags and trim for good measure.

echo "<pre>", print_r($cols), "</pre>";
?>

 

Just remember it does need the code to populate the entries array to fully work.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.