title: Changing Specific DOM Node PHP
body: <p>I have the following code:</p>
  
  <pre><code>$item = "What I have: &lt;a href='/id/blablabla'&gt;This $oldtext.&lt;/a&gt;";
  $dom = new DOMDocument;
  $dom-&gt;loadHTML($item);
  foreach ($dom-&gt;getElementsByTagName('a') as $node) {
      if(strpos($node-&gt;getAttribute('href'),'/id/') !== false) {
           $oldtext = // ?
           $newtext = "This is old:".$oldtext."And this is new: New.";
           // then replace 'This $oldtext.' in $item as $newtext
      }
  }
  </code></pre>
  
  <p>This checks if any  nodes in my <code>$item</code> have an href containing /id/. If they do, they are supposed to <strong>save the current text, referred to as <code>$oldtext</code></strong> of the node, then <strong>replace the text with new text,</strong> which will use <code>$oldtext</code> in the new text.</p>
  
  <p><strong>EDIT:</strong> I've added what the $item looks like for clarity.</p>
  
  <p>How can I save <code>$oldtext</code> even if the node changes, and change the node itself?</p>