Extract data from HTML table row column

27,819

Using simplehtmldom.php...

<?php

include 'simple_html_dom.php';

$html = file_get_html('thetable.html');

$rows = $html->find('tr');
foreach($rows as $row) {
    echo $row->plaintext;
}

?>

or use 'td'...

<?php

include 'simple_html_dom.php';

$html = file_get_html('thetable.html');

$cells = $html->find('td');
foreach($cells as $cell) {
    echo $cell->plaintext;
}

?>
Share:
27,819
Sourav
Author by

Sourav

Hi, I am Sourav Ghosh :) You can find my projects * AJAX Micro Mini Lib [JS] * Autorun Cleaner [VB.NET] * C Code Completer [VB.NET] * DockBar Develop [VB.NET] * ShoutOut-Twitter [ASP.NET + C#] * Ultra Light CAPTCHA [PHP] * Ultra Light Forum [PHP] * Wallpaper Changer [VB.NET]** @ http://sourceforge.net/users/sourav1989 I'll be really happy if you find them useful :) Thanks :)

Updated on January 03, 2020

Comments

  • Sourav
    Sourav over 4 years

    How to extract data from HTML table in PHP. The data is in this format

    Table 1

    <tr><td class="body" valign="top"><a href="example"><b>DATA</b></a></td><td class="body" valign="top">Data_Text</td></tr>
    

    Table 2

    <tr><th><div id="Data">Data</div></th><td>Data_Text_1</td><td>Data_Text_2</td></tr>
    

    Table 3

    <tr><td width="120"><a href="example" target="_blank">DATA</a></td><td>Data_Text</td></tr>
    

    I want to get the Data & Data_Text or (Data_Text_1 & Data_Text_2) from the 3 tables.
    I've used

    $html = file_get_contents($link);
    $doc = new DOMDocument();
    @$doc->loadHTML($html);
    $xpath = new DOMXPath($doc);
    $nodes  = $xpath->query('//td[]');
    $nodes2 = $xpath->query('//td[]');
    

    But it cant show any data !

    I'll offer bounty for this question on day after tomorrow

    • Dimitre Novatchev
      Dimitre Novatchev about 12 years
      There seems to be some mistake: You cannot obtain "Data_Text" from Table 2 -- it doesn't have a text node with such string value. Please, edit and correct.