title: DOMDocument::replaceChild() responds results in 500 (Internal Server error)
body: <p>I have a problem. PHP file responds 500 Error. Can't understand what is the problem. The PHP error message is:</p>
  
  <blockquote>
    <p>Fatal error: Uncaught exception 'DOMException' with message 'Not Found Error' </p>
  </blockquote>
  
  <p>Code is below:</p>
  
  <pre><code>$dom = new domDocument;
  $dom-&gt;preserveWhiteSpace = false;
  $dom-&gt;loadHTML($cont);
  
  $links  = $dom-&gt;getElementsByTagName('a');
  $images = $dom-&gt;getElementsByTagName('img');
  $divs   = $dom-&gt;getElementsByTagName('div');
  
  foreach ($links as $link)
  {
      $link-&gt;setAttribute('target','_blank');
  }
  
  foreach ($images as $image)
  {
      $image-&gt;setAttribute('style','max-width:405px');
  }
  
  foreach ($divs as $div)
  {
      $pnode = $dom-&gt;createElement('p', $div-&gt;nodeValue);
      $dom-&gt;replaceChild($pnode, $div); //the problem string
  }
  </code></pre>
  
  <p>Update: If the problem string is commented there are no errors.</p>