I have a problem with a php DOM Object
http://php.net/manual/en/class.domdocument.php
Is it possible only to show content from the third tag and the second tag in that table?
/*** a new dom object ***/
$dom = new domDocument;
/*** load the html into the object ***/
@$dom->loadHTML($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.'<br />';
echo $cols->item(3)->nodeValue.'<br />';
echo $cols->item(4)->nodeValue.'<br />';
echo $cols->item(5)->nodeValue.'<br />';
echo '<hr />';
}
EDIT:
I get this error: Fatal error: Cannot use object of type DOMNodeList as array in
<?php
/*** a new dom object ***/
$dom = new domDocument;
/*** load the html into the object ***/
@$dom->loadHTML('content.html');
/*** discard white space ***/
$dom->preserveWhiteSpace = false;
$xpath = new DOMXPath($dom);
$selected = $xpath->query('//table/tr/td[first()+1]');
echo $selected[0]->nodeValue;
?>
Edit2:
<?php
$output = file_get_contents('test.php');
/*** a new dom object ***/
$dom = new domDocument;
/*** load the html into the object ***/
@$dom->loadHTML($output);
/*** discard white space ***/
$dom->preserveWhiteSpace = false;
/*** the table by its tag name ***/
$tables = $dom->getElementsByTagName('table');//get all the tables
if($tables->length > 2) { //check there are more than 2
$thirdTable = $tables->item(2);
$cols = $thirdTable->getElementsByTagName('td');
/*** echo the values ***/
echo $cols->item(0)->nodeValue.'<br />';
echo $cols->item(1)->nodeValue.'<br />';
echo $cols->item(2)->nodeValue.'<br />';
echo $cols->item(3)->nodeValue.'<br />';
echo $cols->item(4)->nodeValue.'<br />';
echo $cols->item(5)->nodeValue.'<br />';
echo '<hr />';
}
?>
EDIT3 – This code only shows content from the third table tag. But it also need only to show content from the second tr tag in the third table.
$html = file_get_contents('content.html');
/*** a new dom object ***/
$dom = new domDocument;
/*** load the html into the object ***/
@$dom->loadHTML($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(2)->getElementsByTagName('tr')->item(1);
/*** 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.'<br />';
echo $cols->item(3)->nodeValue.'<br />';
echo $cols->item(4)->nodeValue.'<br />';
echo $cols->item(5)->nodeValue.'<br />';
echo '<hr />';
}
I don’t understand your problem. With
$cols->item(2)you got the second DOMElement you need.If you just want the first (or the second…) you can use XPath
If you do not want to use DOMXPath, you can stay with your getElementsByTagName
First you get all the tables
then you check there are more than 2
then you take the third
then you take the tr element
you keep in an array the second and the third