Monday 6 November 2017

Parse HTML Table - PHP

Take a look at the href="http://simplehtmldom.sourceforge.net/" rel="nofollow noreferrer">PHP HTML DOM
Parser library.



To use, you can do
something similar to this (not my
example):




require('simple_html_dom.php');

$table
= array();

$html =
file_get_html('http://flow935.com/playlist/flowhis.HTM');
foreach($html->find('tr')
as $row) {
$time = $row->find('td',0)->plaintext;
$artist =
$row->find('td',1)->plaintext;
$title =
$row->find('td',2)->plaintext;



$table[$artist][$title] = true;
}

echo
'
';
print_r($table);
echo
'
';


There's
some tutorials, SO questions and interesting reads about the library. It seems to be
pretty
popular.






UPDATE
FOR FINDING SPECIFIC TABLE IN HTML USING ABOVE
LIBRARY



To find a particular
table amongst many:



1. By
class:



On line 465 of your
scraped HTML, the table starts with a class catalog-listing,
so:




foreach
($html->find('table[@class="catalog-listing"]')->find('tr') as $row) {

// extract TD
data
}


2.
By instance
(find 2nd table in
HTML)



foreach
($html->find('table', 2)->find('tr') as $row) {
// extract TD
data
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...