Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In order to see the content of an object in this manner, you would need to convert it to a string. If you wanted to do this, you could either simply <code>echo</code>/<code>print</code> it, or cast it to a string like this:</p> <pre><code>$string = (string) $object; </code></pre> <p>When an object is converted to a string, PHP calls it's <code>__toString()</code> method. Unfortunately a DOMElement object does not define one, so when you try and convert it to a string you will get the following error:</p> <blockquote> <p>Catchable fatal error: Object of class DOMElement could not be converted to string in...</p> </blockquote> <p>In this specific scenario, there is a poor man's work around that allows one to do this in a small amount code, and that is to bounce it through <a href="http://php.net/manual/en/book.simplexml.php" rel="nofollow">SimpleXML</a> using the <a href="http://php.net/manual/en/function.simplexml-import-dom.php" rel="nofollow"><code>simplexml_import_dom()</code></a> function - since the <code>SimpleXMLElement</code> class does define an <code>asXML()</code> method which will convert it back to an XML string. So you can do this:</p> <pre><code>foreach($headerh1 as $match) { $xml = simplexml_import_dom($match); echo $xml-&gt;asXML(); } </code></pre> <p>However, in this specific instance, this results in the following output:</p> <pre><code>&lt;h1 id="h-top-questions"&gt;&amp;#13; Top Questions &amp;#13; &lt;/h1&gt; </code></pre> <p>So you see, there are some extra HTML entities leaking in there that were not in the original source code. This has happened somewhere between DOM and SimpleXML. </p> <p>In order to get around this, it would be possible to:</p> <ul> <li>Pass the output through <a href="http://php.net/manual/en/function.html-entity-decode.php" rel="nofollow"><code>html_entity_decode()</code></a>. This is not a great option as it may result in invalid XML/HTML output.</li> <li>Roll your own function to convert a DOMElement to a string. This is also not a great option as it would be very complex, and you would potentially need to recurse through many levels to successfully retrieve a full node list.</li> </ul> <p>However, one thing to keep in mind here is that what you are doing does not have much of a practical application for anything other than testing and debugging. One would never really want to output a partial document.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload