How to parse an HTML table using PHP?

Possible duplicate:
How to parse and process HTML using PHP?

I need to get the second column of a given HTML table using PHP. How can i do this?

Literature:

Analysis table: http://bit.ly/Ak2xay

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

+7
source share
4 answers

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

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

To find the values โ€‹โ€‹of the second coloumn for a given table after the DOM is executed, it 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]; } ?> 

Link: Configure the code provided in How to parse this table and extract data from it? to fit this issue. p>

+11
source

It may be useful for you, there are even examples to get you started.

http://simplehtmldom.sourceforge.net/

0
source

Using phpQuery http://code.google.com/p/phpquery/ you can do

 $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. 
0
source

Perhaps take a look at phpQuery: http://code.google.com/p/phpquery/ ?
I have not used it myself, so I am not 100% sure that it does what you want, but since it is a jQuery server side implementation to select from the DOM using CSS selectors, I think it might be useful in your happening.

-one
source

All Articles