Sunday, 15 December 2019

php - Parse HTML table and extract data

I found this topic:
How to parse this table and extract data from it?

Which probably is the answer for my problem, I want to the same thing - parse some external table, extract data and print this data on my webpage.
@Yoshi gives perfect answer, but when I trying to use this code, I am getting empty array




Array ( )




I don't know why this doesn't work? I trying to use the same example




$dom = new DomDocument;
$dom->loadHtmlFile('http://www.nbs.rs/kursnaListaModul/srednjiKurs.faces?lang=lat');

$xpath = new DomXPath($dom);

// collect header names
$headerNames = array();
foreach ($xpath->query('//table[@id="index:srednjiKursLista"]//th') as $node) {
$headerNames[] = $node->nodeValue;
}


// collect data
$data = array();
foreach ($xpath->query('//tbody[@id="index:srednjiKursLista:tbody_element"]/tr') as $node) {
$rowData = array();
foreach ($xpath->query('td', $node) as $cell) {
$rowData[] = $cell->nodeValue;
}

$data[] = array_combine($headerNames, $rowData);

}

print_r($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 ...