title: Alternatives to PHP getElementById() of DOMDocument?
body: <p><strong>EDIT</strong>: related to <a href="https://stackoverflow.com/questions/3391942/php-html-domdocument-getelementbyid-problems">PHP HTML DomDocument getElementById problems</a></p>
  
  <p>Can't get <code>getElementById()</code> to work. Document is valid, according to using W3C validator. Plus my code should work even with a little invalid HTML.</p>
  
  <p>This simple code looks for an image with <code>id="banner"</code> and replace it's <code>src</code> attribute with another one. Works on my development machine (Windows), doesn't work on server (Ubuntu). </p>
  
  <p>Any idea of how to do this without <code>getElementById()</code>?</p>
  
  <pre><code>    libxml_use_internal_errors(true);
  
      // Create the DOMDocument and get the HTML content
      $document = new \DOMDocument();
  
      // Load HTML string and if it fails just return the content itself
      if(false === $document-&gt;loadHTML($content)) return $content;
  
      // Get DOMElement of the image with id="banner"
      $img = $document-&gt;getElementById('banner');
  
      // Return the content if it can't find the image
      if(null === $img) return $content;
  
      // Get image parent and remove the banner from DOM
      $parent = $img-&gt;parentNode;
      $parent-&gt;removeChild($img);
  
      // Set the new src attribute
      $img-&gt;setAttribute('src', 'http://mysite.come/img/myimage.png');
  
      // Append the modified node to banner parent
      $parent-&gt;appendChild($img);
  
      return $document-&gt;saveHTML();
  </code></pre>