Find and extract content of division of certain class using DomXPath
<p>I am trying to extract and save into PHP string (or array) the content of a certain section of a remote page. That particular section looks like:</p>

<pre><code>&lt;section class="intro"&gt;
        &lt;div class="container"&gt;
            &lt;h1&gt;Student Club&lt;/h1&gt;
            &lt;h2&gt;Subtitle&lt;/h2&gt;
            &lt;p&gt;Lore ipsum paragraph.&lt;/p&gt;
        &lt;/div&gt;
&lt;/section&gt;
</code></pre>

<p>And since I can't narrow down using class container because there are several other sections of class "container" on the same page and because there is the only section of class "intro", I use the following code to find the right division:</p>

<pre><code>$doc = new DOMDocument;
$doc-&gt;preserveWhiteSpace = FALSE;
@$doc-&gt;loadHTMLFile("https://www.remotesite.tld/remotepage.html");
$finder = new DomXPath($doc);
$intro = $finder-&gt;query("//*[contains(@class, 'intro')]");
</code></pre>

<p>And at this point, I'm hitting a problem - can't extract the content of $intro as PHP string. </p>

<p>Trying further the following code</p>

<pre><code>foreach ($intro as $item) {
                    $string = $item-&gt;nodeValue;
                    echo $string;
                }
</code></pre>

<p>gives only the text value, all the tags are stripped and I really need all those divs, h1 and h2 and p tags preserved for further manipulation needs.</p>

<p>Trying:</p>

<pre><code>foreach ($intro-&gt;attributes as $attr) {
                $name = $attr-&gt;nodeName;
                $value = $attr-&gt;nodeValue;
                echo $name;
                echo $value;
            }
</code></pre>

<p>is giving the error:</p>

<pre><code>Notice: Undefined property: DOMNodeList::$attributes in 
</code></pre>

<p>So how could I extract the full HTML code of the found DOM elements?</p>