1

file.html

<div>
    <a href="">apple</a>
</div> 


$html = new DOMDocument();
$html->preserveWhiteSpace = true;
$html->loadHTML( file_get_contents('file.html') );
$nodes =  $html->getElementsByTagName('*');

foreach($nodes as $i=>$node) {
     if($node->nodeName == 'div')
     echo $node->nodeValue;
}


this returns 'apple'. How do I get the child node including the child node's value, as in:
<a href="#">apple</a>

5
  • 3
    What you want is the 'innerHTML' of the node, which PHP DOM doesn't support. php.net/manual/en/book.dom.php#89802 details how to fake it. Commented May 19, 2011 at 18:05
  • @Marc B: Nice! Though I wish I didn't have to "fake" it Commented May 19, 2011 at 18:14
  • @Marc B: You should answer with that so I can upvote you. Commented May 19, 2011 at 18:14
  • @MarcB There is a way, it's just not as obvious. See the answer I just posted. Commented May 19, 2011 at 18:14
  • Thanks, but onteria's answer looks to be a much cleaner solution. Commented May 19, 2011 at 18:31

1 Answer 1

2

You can pass the a dom node to DOMDocument::saveXML and it will spit out the actual HTML instead:

$html = new DOMDocument();
$html->preserveWhiteSpace = true;
$html->loadHTML( file_get_contents('file.html') );
$nodes =  $html->getElementsByTagName('*');

foreach($nodes as $i=>$node) {
     if($node->nodeName == 'div') {
       //Navigate to the specific element you want
       //then pass it to saveXML
       echo $html->saveXML($node->childNodes->item(1));
     }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.