How to parse HTML table using PHP?

Possible Duplicate:
How to parse and process HTML with PHP?

I need to fetch the second column of the given HTML table using PHP. How can I do it?

References:

Table to be parsed: http://bit.ly/Ak2xay

HTML code of this table: http://bit.ly/ACdLMn


For tidy HTML codes, one of the parsing approach can be DOM. DOM divides your HTML code into objects and then allows you to call the desired object and its values/tag name etc.

The official documentation of PHP HTML DOM parsing is available at http://php.net/manual/en/book.dom.php

For finding the values of second coloumn for the given table following DOM implementation can be done:

<?php
$data = file_get_contents('http://mytemporalbucket.s3.amazonaws.com/code.txt');

$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');
        echo $cols[2];
}

?>

Reference: Customized the code provided at How to parse this table and extract data from it? to match this question's demand.


This may be of use to you, there are even examples to get you started.

http://simplehtmldom.sourceforge.net/


使用phpQuery http://code.google.com/p/phpquery/你可以

$file = LINK OR NAME OF YOUR FILE
phpQuery::newDocumentFile($file);
$data = pq('UNIQUE COLUMN ID OR CLASS AS YOU WOULD FOR CSS ex: .class #id')->html();

echo $data.
链接地址: http://www.djcxy.com/p/29900.html

上一篇: PHP解析HTML标签

下一篇: 如何使用PHP解析HTML表格?