How to parse this table and extract data from it?

I have the following table: http://www.nbs.rs/kursnaListaModul/srednjiKurs.faces?lang=lat

This is a currency exchange list, and I need to extract some data from it. Currency identification numbers are shown on the left side of the table. Is it possible to extract data from specified rows based on their identifiers?

For example, from the above table I want to extract currencies with identifiers 978, 203 and 348.

The conclusion should be:

  • EUR 104,2182
  • CZK 4.2747
  • HUF 38.7919

Having looked at similar examples, I came up with the following: http://pastebin.com/hFZs1H7C

I need to somehow find the identifiers and the correct print values ​​... I know when it comes to programming, and I need your help.

<?php $data = file_get_contents('http://www.nbs.rs/kursnaListaModul/srednjiKurs.faces?lang=lat'); $dom = new domDocument; @$dom->loadHTML($data); $dom->preserveWhiteSpace = false; $tables = $dom->getElementsByTagName('table'); $rows = $tables->item(1)->getElementsByTagName('tr'); foreach ($rows as $row) { $cols = $row->getElementsByTagName('td'); foreach ($cols as $col) { echo $col; } } ?> 
+4
source share
2 answers

Collecting table data as an array for later use:

 $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); 

Exit

 Array ( [0] => Array ( [ŠIFRA VALUTE] => 978 [NAZIV ZEMLJE] => EMU [OZNAKA VALUTE] => EUR [VAŽI ZA] => 1 [SREDNJI KURS] => 104,2182 ) ... ) 

Usage example:

 foreach ($data as $entry) { printf( '%s %s' . PHP_EOL, $entry['OZNAKA VALUTE'], $entry['SREDNJI KURS'] ); } 
+10
source

You can use the xpath and domdocument PHP functions to extract certain data from html (or xml.)

 $src = new DOMDocument('1.0', 'utf-8'); $src->formatOutput = true; $src->preserveWhiteSpace = false; $content = file_get_contents("http://www.nbs.rs/kursnaListaModul/srednjiKurs.faces?lang=lat"); @$src->loadHTML($content); $xpath = new DOMXPath($src); $values=$xpath->query('//td[ contains (@class, "tableCell") ]'); foreach($values as $value) { echo $value->nodeValue."<br />"; } 

this will print innerHTML for each td element with class = "tableCell".

+3
source

All Articles