title: Get DOM element string using PHP
body: <p>I have a set of html strings that can look like this:</p>
  
  <pre><code>&lt;div id="myelementID" class="hello" data-foo="bar"&gt; ... &lt;/div&gt;
  </code></pre>
  
  <p>or</p>
  
  <pre><code>&lt;div id="myelementID" class="world" data-this="that"&gt; ... &lt;/div&gt;
  </code></pre>
  
  <p>etc etc, you get the idea. Except for <code>id="myelementID"</code>, every other attribute else is not fixed.</p>
  
  <p>What I need is to extract the exact string of the the <code>&lt;div&gt;</code>, eg. <code>&lt;div id="myelementID" class="hello" data-foo="bar"&gt;</code> if an element with the ID "myelementID" exists.</p>
  
  <p>As of now, I'm able to use DomDocument to check if the element exists:</p>
  
  <pre><code>        $dom = new DomDocument;
          $dom-&gt;validateOnParse = true;
          $internalErrors = libxml_use_internal_errors(true);
          $dom-&gt;loadHTML($html_string);
          libxml_use_internal_errors($internalErrors);
          $el = $dom-&gt;getElementById("myelementID");
  </code></pre>
  
  <p>From here, how can I get the element's HTML string? I'm open to using preg_match as well, which may be an even better solution.</p>
  
  <p><strong>edit</strong> 
  Just to be clearer, I'm not looking for the content of the element. I'm looking for the string <code>&lt;div id="myelementID" etc="etc" this="that"&gt;</code>. Because it's not certain what attributes the element has apart from the fact that it's ID is "myelementID", that's why I'm having the problem.</p>