Skip to content Skip to sidebar Skip to footer

How Can I Extract Data From An Html Table In Php?

Possible Duplicate: How to parse and process HTML with PHP? Let's say I want to extract a certain number/text from a table from here: http://www.fifa.com/associations/associatio

Solution 1:

Try http://simplehtmldom.sourceforge.net/,

$html = file_get_html('http://www.google.com/');
echo$html->find('div.rankings', 0)->find('table', 0)->find('tr',0)->find('td.c',0)->plaintext;

This is untested, just looking at the source. I'm sure you could target it faster.

In fact,

echo$html->find('div.rankings', 0)->find('td.c',0)->plaintext;

should work.

Solution 2:

Using DOMDocument, which should be pre-loaded with your PHP installation:

$dom = new DOMDocument();
$dom->loadHTML(file_get_contents("http://www.example.com/file.html"));
$xpath = new DOMXPath($dom);
$cell = $xpath->query("//td[@class='c']")->item(0);
if( $cell) {
    $number = intval(trim($cell->textContent));
    // do stuff
}

Post a Comment for "How Can I Extract Data From An Html Table In Php?"