How to use DOMDocument() to iterate over all root HTML elements in a variable?

Good morning!

There is a variable `$content` with the following content:

$content = <<<eod <div id="foo">

<pre class="bar"> <code>
 echo "Hello, World!";

<p id="third">Hello.</p>

A text node.

 <table id="fourth">

Another text node.

You need to get all the root elements (along with their content), including text, into an array, so then they take turns treating.

Something like this:

 <div id="foo">All the content of the element</div>
 <pre class="bar">All the content of the element</pre>,
 <p id="third">All the content of the element</p>
 A text node
 All the content of the element<table id="fourth"></table>
 Another text node.,


$dom = new DOMDocument( '1.0', 'UTF-8' );
$dom->loadHTML( $content );
$xpath = new DOMXPath( $dom );
$tags = $xpath->query('/html/body/*');
foreach ( $tags as $node ) {
 echo ???;

Thank you very much!
August 19th 19 at 22:54
1 answer
August 19th 19 at 22:56
the problem, IMHO, is pointless, but there you go.
Thanks, but still the array get text nodes ( - alfonzo.Bernhard commented on August 19th 19 at 22:59
: hike, learn, yourself you by no means want (( - theodore_Durgan commented on August 19th 19 at 23:02
Thank you very much! Now this will be a start. The last question that here (comment above) mean?
[c]And why? Why not work in a loop with each element as an object DOMElement/DOMNode?[/c] - alfonzo.Bernhard commented on August 19th 19 at 23:05
: you're going to these elements as any further processing. it makes no sense to collect them separately in an array (especially in the html form), if all of these elements so you already have inside the loop in the form of a DOMElement/DOMNode (or hypertext markup language, if you really must) that you can safely handle.. - theodore_Durgan commented on August 19th 19 at 23:08

Find more questions by tags PHP