How to read html table tag in php?

<?php
// new dom object
$dom = new DOMDocument();

//load the html
$html = $dom->loadHTMLFile(“test.html”);

//discard white space
$dom->preserveWhiteSpace = false;

//the table by its tag name
$tables = $dom->getElementsByTagName(‘table’);

//get all rows from the table
$rows = $tables->item(0)->getElementsByTagName(‘tr’);

// loop over the table rows
foreach ($rows as $row)
{
// get each column by tag name
$cols = $row->getElementsByTagName(‘td’);
// echo the values
echo $cols->item(0)->nodeValue.'<br />’;
echo $cols->item(1)->nodeValue.'<br />’;
echo $cols->item(2)->nodeValue;
}

?>


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *