2

With the code below I can get elements of an html table using DOM's getElementsByTagName and it works.

$dom = new DOMDocument();
$dom->loadHTMLFile("any.html");

## get table by tagName

$tables= $dom->getElementsByTagName('table');
$table = $tables->item(0);

foreach($table->getElementsByTagName('tr') as  $key =>$tr){
     $tr->getElementsByTagName('td')->item(0)->nodeValue;
}

but I want to get table by getElementById. Is it possible by using table's id?

mustafa
  • 737
  • 3
  • 9
  • 24

1 Answers1

2

Actually you need to do this first:

$doc->validateOnParse = true;

then

$tableId = 'someId';
$table = $dom->getElementById($tableId);

foreach($table->getElementsByTagName('tr') as  $key =>$tr){
     $tr->getElementsByTagName('td')->item(0)->nodeValue;
}
Daniel A. White
  • 181,601
  • 45
  • 354
  • 430