13

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

Community
  • 1
  • 1
isiaatz
  • 1,125
  • 4
  • 15
  • 22

4 Answers4

22

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 column 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.

albert
  • 8,112
  • 3
  • 47
  • 63
codersofthedark
  • 9,183
  • 8
  • 45
  • 70
1

Using phpQuery http://code.google.com/p/phpquery/ you could 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.
Clark T.
  • 1,470
  • 2
  • 11
  • 25
-1

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

http://simplehtmldom.sourceforge.net/

Geoffrey
  • 10,843
  • 3
  • 33
  • 46
  • if you are not going to explain how to do it, there is very little value in recollecting what's already given at http://stackoverflow.com/questions/3577641/best-methods-to-parse-html – Gordon Jan 11 '12 at 09:20
  • Why give examples and explain when the link provided already explains and gives examples? – Geoffrey Jan 11 '12 at 09:29
  • 2
    Because the link may go away and we already have a canonical collection of libraries to link to. Also see http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers – Gordon Jan 11 '12 at 09:43
-3

Maybe have a look at phpQuery: http://code.google.com/p/phpquery/?
I haven't used it myself so I'm not 100% sure it does what you want, but since it is a server-side implementation of jQuery to select from the DOM, using CSS selectors, I think it could be useful in your case.

jeroen
  • 1,672
  • 1
  • 12
  • 11
  • if you are not going to explain how to do it, there is very little value in recollecting what's already given at http://stackoverflow.com/questions/3577641/best-methods-to-parse-html – Gordon Jan 11 '12 at 09:20