Jump to content

Parse a table from a website with preg_match


yujikaido

Recommended Posts

Could anyone please tell what I am doing wrong with my code? I just get a blank page when I echo output  and run it on xamplite. Thanks

<?
$url = "http://www.cs.iusb.edu/people/faculty.html"; 
$input = @file_get_contents($url) or die('Could not access file: $url'); 
$pat = "/id=\table1\" .+\<tbody\>(.+)\<\/tbody\>/s";
   preg_match($pat,$input,$outp);
echo $outp[1];
?>

 

Link to comment
Share on other sites

Ok I got it to output the html table I wanted but it lost its table format.  this is what I want my end result to look like at this address http://www.cs.iusb.edu/~rbatzing/a340/faculty.php so I want to grab the first table "table1" in the body. Thanks

 

<?
$url = "http://www.cs.iusb.edu/people/faculty.html"; 
$input = file_get_contents($url) or die('Could not access file: $url'); 
   if (preg_match("/id=\"table1\" .+?\<tbody\>(.+?)\<\/tbody\>/s",$input,$outp));
echo $outp[1];
?>

Link to comment
Share on other sites

I now got it to pull the table correctly now I somehow have to change the table color and instead of the table being horizonal  it has to be vertically stacked with faculty on top Ass. faculty in the middle and staff on the bottom. Does anybody have any idea how I might go about this? Thanks

 

 

Link to comment
Share on other sites

Thanks for the reply and the help. I tried the line but i get: Warning: preg_match_all() expects parameter 2 to be string, array given in /Applications/XAMPP/xamppfiles/htdocs/falculty.php on line 5"  when I run it. any suggestions ?

 

<?
$url = "http://www.cs.iusb.edu/people/faculty.html"; //set the web address to an array
$input = file_get_contents($url) or die('Could not access file: $url'); // input get set to grab the contents of the page or else displays msg.
   if (preg_match("/<table id=\"table1\" .*?\<tbody\>(.*?)\<\/tbody\>/s",$input,$outp));// preg match first sets the expression to grap table1 and then processing it with $input array and sets the result to $outp array
   
   preg_match_all("/(<ul.*>)(\w.*)(<\/ul>)/isxmU",$outp,$matches); 
echo $matches[0];// ouputs the completed table
echo $matches[1];// ouputs the completed table
echo $matches[2];// ouputs the completed table
?>


Link to comment
Share on other sites

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.