14

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?

2
  • yes, and get an array of columns Commented Jan 11, 2012 at 8:31
  • 1
    @everyone who marked this as a duplicate... I don't see anything about parsing html TABLES on that page. Care to elaborate? Commented Dec 9, 2013 at 21:43

2 Answers 2

23

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Would this be in the same file as the code where you want to implement and use this data?
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.