title: Trying to use PHP DOM to replace node text without changing child nodes
body: <p>I am trying to use the dom object to simplify the implementation of a glossary tooltip. What I need to do is to replace a text element in a paragraph, but NOT in an anchor tag that may be embedded in the paragraph.</p>
  
  <pre><code>$html = '&lt;p&gt;Replace this tag not this &lt;a href="#"&gt;tag&lt;/a&gt;&lt;/p&gt;';
  $document = new DOMDocument();
  $document-&gt;loadHTML($html);
  $document-&gt;preserveWhiteSpace = false;
  $document-&gt;validateOnParse = true;
  
  $nodes = $document-&gt;getElementByTagName("p");
  foreach ($nodes as $node) {
    $node-&gt;nodeValue = str_replace("tag","element",$node-&gt;nodeValue);
  }
  echo $document-&gt;saveHTML();
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>'...&lt;p&gt;Replace this element not this element&lt;/p&gt;...'
  </code></pre>
  
  <p>I want:</p>
  
  <pre><code>'...&lt;p&gt;Replace this element not this &lt;a href="#"&gt;tag&lt;/a&gt;&lt;/p&gt;...'
  </code></pre>
  
  <p>How do I implement this such that only the parent node text is changed and the child node (a tag) is not changed?</p>