I was using this question about this matter.
How to parse this table and extract data from it?
But got stumped on the table I am trying to parse.
This is the PHP page source code.
There is only one table in it, table id "troops".
I managed to get the table headers on an array, but can't connect the row data with the headers.
This is the code I am using, its for the article above, edited to my needs.
html source-code
http://pastebin.com/RKbzVT1V
php code used
$content = $_POST['src'];
$dom = new DomDocument;
$dom -> loadHtml($content);
$xpath = new DomXPath($dom);
// collect header names
$headerNames = array();
foreach ($xpath->query('//table[@id="troops"]//th') as $node) {
//foreach ($xpath->query('//th[ contains (@class, "vil fc") ]') as $node) {
$headerNames[] = $node -> nodeValue;
}
// collect data
$data = array();
foreach ($xpath->query('//tr') as $node) {
$rowData = array();
foreach ($xpath->query('//td', $node) as $cell) {
$rowData[] = $cell -> nodeValue;
}
$data[] = array_combine($headerNames, $rowData);
}
Any help on this matter is appreciated, if there is an easier way please advise.
No comments:
Post a Comment