Skip to content Skip to sidebar Skip to footer

Html Dom Document Parsing

i am new to DOM Document.. i have this html: Sun

Solution 1:

$doc = new DOMDocument();
$doc ->loadHTML("$html");
$tables = $doc->getElementsByTagName('table');
$table = $tables->item(0);//takes the first table in domforeach ($table->childNodes as$td) {
  if ($td->nodeName == 'td') {
    echo$td->nodeValue, "\n";
  }
}

Solution 2:

Passing an element to saveHtml generates the elements outerHTML not its innerHTML, so you get its tag attributes and all its content. Of course you need to be running PHP>=5.3.6 .

The values between the td can be obtained by $td->firstChild->nodeValue; or just $td->textContent; where $td is the <td> in question.

Post a Comment for "Html Dom Document Parsing"