Jump to content

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


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.