Concatenate HTML tables with PHP DOMDocument -
i have whole bunch of large html documents tables of data inside , i'm looking write script can process html file, isolate tags , contents, concatenate rows within tables 1 large data table. loop through rows , columns of new large table.
after research i've started trying out php's domdocument class parse html wanted know, best way this?
this i've got far...
$dom = new domdocument(); $dom->preservewhitespace = false; @$dom->loadhtmlfile('exrate.html'); $tables = $dom->getelementsbytagname('table');
how chop out other tables , contents? i'd remove first table since it's table of contents. loop through table rows , build them 1 large table.
anyone got hints on how this? i've been digging through docs domdocument on php.net i'm finding syntax pretty baffling!
cheers, b
edit: here sample of html file data tables i'd join http://thenetzone.co.uk/exrates/exrate.html
ok got sorted phpquery , lots of trial , error.
takes whole bunch of tables , moves contents first one, removes empty tables.
loops through each table row , extracts text specific columns, in case 2nd , 3rd td of each row.
require('phpquery/phpquery.php'); $doc = phpquery::newdocumentfilehtml('exrates_code.html'); pq('table:first')->remove();// remove first table, contents table not interested pq('tr:has(th)')->remove();// remove table rows headers pq('table:not(:first) tr')->appendto('table:first');// move contents of other tables first pq('table:empty')->remove();// remove empty tables pq('br')->remove(); $rows = pq('table tr'); foreach ($rows $row) { $currency = pq($row)->find('td:eq(1)')->text(); $value = pq($row)->find('td:eq(2)')->text(); }
hope helps out!
Comments
Post a Comment