Get innerHTML of DOMNode

We can retrieve the inner HTML of a DOMNode using the C14N() method with an XPath query.

The C14N() method is used to serialize a DOMNode to a string. By default, it serializes the entire node and its contents, including the opening and closing tags. However, passing some additional arguments allows us to control exactly what gets serialized.

<?php

$dom = new DOMDocument();
$dom->loadHTML('<div><p>Hello, world!</p><ul class="list"><li>Item 1</li><li>Item 2</li></ul></div>');

$node = $dom->getElementsByTagName('div')[0];

$innerHtml = $node->C14N(
    true, // parse only xpath query nodes
    false, // no comments
    ["query" => ".//node()|.//*//@*"] // xpath query to select innerHTML nodes  & attributes
);

echo $innerHtml; // Output: <p>Hello, world!</p><ul class="list"><li>Item 1</li><li>Item 2</li></ul>

In this code, we're using the C14N() method with the following arguments:

  • The first argument (true) specifies that we want to serialize only the nodes matched by an XPath query.
  • The second (false) argument specifies that we don't want to include comments in the resulting string.
  • The third argument (["query" => ".//node()|.//*//@*"]) specifies the XPath query to select all child nodes and attributes of a DOMNode.

More Readings!