title: PHP DomNode->insertBefore()
body: <p>I am trying to insert nodes in my html string.
  My goal is to insert an element before each h2 tag.</p>
  
  <p>For that, I am using:</p>
  
  <pre><code>$htmlString = "&lt;h2&gt;some html&lt;/h2&gt;";
  
  $DOM = new DOMDocument();
  $DOM-&gt;loadHTML($htmlString);
  
  $itemTitles = $DOM-&gt;getElementsByTagName('h2');
  
  for($i = 0; $i &lt; $itemTitles-&gt;length; $i ++)
  {
      $helpNavigatorContents[] = $itemTitles-&gt;item($i)-&gt;nodeValue;
      $textBefore = new DOMNode(
          '&lt;a name="'.$itemTitles-&gt;item($i)-&gt;nodeValue.'"&gt;&lt;/a&gt;'
      );
      $itemTitles-&gt;item($i)-&gt;parentNode-&gt;insertBefore(
          $textBefore, 
          $itemTitles-&gt;item($i)
      );
  }
  
  $htmlString = $DOM-&gt;saveHTML($DOM);
  </code></pre>
  
  <p>And here I have a problem with the <code>$textBefore</code>. When I declare the <code>$textBefore</code> as a <code>DOMText</code>, I can insert the text before the node but when I try this with <code>DOMNode</code>, then I am getting the following error (<a href="https://eval.in/34679" rel="nofollow">Demo</a>):</p>
  
  <blockquote>
    <p>Warning: DOMNode::insertBefore(): Couldn't fetch DOMNode</p>
  </blockquote>