title: In PHP, how can I use the DOMDocument class to replace the src= attribute of an IMG tag?
body: <p>It's often useful to be able to swap out the src= attribute of an HTML IMG tag without losing any of the other attributes. What's a quick, non-regex way of doing this?</p>
  
  <p>The reasons I don't want to use RegEx are:</p>
  
  <ol>
  <li>It's not very readable. I don't want to spend 20 mins deciphering a pattern every time I need to account for a new case. </li>
  <li>I am planning on modifying this function to add in width and height attributes when they're missing. A simple RegEx string replacement won't be easy to modify for this purpose.</li>
  </ol>
  
  <p>Here's the context: I have a bunch of RSS feed posts that each contain one image. I would like to replace these images with blank images, but keep the HTML otherwise unaffected:</p>
  
  <pre><code>$raw_post_html = "&lt;h2&gt;Feed Example&lt;/h2&gt;
      &lt;p class='feedBody'&gt;
          &lt;img src='http://premium.mofusecdn.com/6ff7098b3c8561d70c0af16d30e57d4e/cache/other/48da8425bc54af2d5d022f28cc8b021c.200.0.0.png' alt='Feed Post Image' width='350' height='200' /&gt;
          Feed Body Content
      &lt;/p&gt;";
  
  echo replace_img_src($raw_post_html, "http://cdn.company.org/blank.gif");
  </code></pre>