Search code examples
phphtml-tabledomdocumentdomxpath

Get td values with loadHTML and DOMXPath


I'm trying to get the td values from a table, using loadHTML and DOMXPath, but even though there are no errors, the nodes always return empty on both "textContent" and "nodeValue".

The table id is "akas", but nothing else inside the table has id's or classes.

Here's my code:

$xml = new DomDocument();
$xml->validateOnParse = true;
libxml_use_internal_errors(true);
$xml->loadHTML($data); //$data contains all the HTML

$xpath = new DOMXPath($xml);
$table = $xpath->query('//table[@id="akas"]')->item(0);

$rows = $table->getElementsByTagName("tr");

foreach($rows as $row)
{
  $cells = $row -> getElementsByTagName('td');
  foreach ($cells as $cell) print $cell->nodeValue;
}

There are no errors, but I can't get anything from the nodes, if I do a var_dump every attribute returns empty, except for the [tagName] and [nodeName]. "Length" of the table returns the correct value (the number of tr's and/or td's inside).

What could be wrong? Can anyone shed some light on this?


Solution

  • I forgot to turn on CURLOPT_RETURNTRANSFER on the curl code to get the html, so if I did a var_dump of $data the html was there, but if I tried to do anything with the contents it would always return empty. :P

    Thansk for your help anyway hek2mgl.