DOMDocument not making HTML tags into DOMNodes

67 views Asked by At

I am trying to manipulate some HTML data with the DOMDocument class, however it does not appear to be working as expected.

$html_str = <<< 'EOD'
<h1>Hello world</h1>
<p>This is the first paragraph</p>
<p>Now for a second</p>
<p>This is the last</p>
EOD;

$html_doc = new DOMDocument();
$html_doc->loadHTML( $html_str ); //loadHTML wraps code in html and body nodes
$body_node = $html_doc->getElementsByTagName( 'body' )->item( 0 ); //Select body node which is holding loaded html data

echo $html_doc->saveHTML( $body_node );

//Outputs as expected
//<body>
//<h1>Hello world</h1>
//<p>This is the first paragraph</p>
//<p>Now for a second</p>
//<p>This is the last</p>
//</body>

echo count( $body_node->childNodes );

//Outputs only 1

echo $html_doc->saveHTML( $body_node->childNodes->item( 0 ) );

//Outputs
//<h1>Hello world</h1>

Why isn't the HTML data I loaded recognised as individual node. I want to be able to manipulate each of those nodes that are inside that body tag but as you can see I can't target them using the cildeNodes property.

1

There are 1 answers

0
Alf Eaton On

$body_node->childNodes is a DOMNodeList object, which does not implement the Countable interface. Use $body_node->childNodes->length instead of count($body_node->childNodes).

DOMNodeList does implement the Traversable interface, though, so you can loop through the child nodes using foreach.