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];
?>

 

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];
?>

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

 

 

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
?>


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.