title: PHP Regex Replace Image SRC Attribute
body: <p>I'm trying to find a regular expression that would allow me replace the <code>SRC</code> attribute in an image. Here is what I have:</p>
  
  <pre><code>function getURL($matches) {
    global $rootURL;
    return $rootURL . "?type=image&amp;URL=" . base64_encode($matches['1']);
  }
  
  $contents = preg_replace_callback("/&lt;img[^&gt;]*src *= *[\"']?([^\"']*)/i", getURL, $contents);
  </code></pre>
  
  <p>For the most part, this works well, except that anything before the <code>src="</code> attribute is eliminated when $contents is echoed to the screen. In the end, <code>SRC</code> is updated properly and all of the attributes after the updated image URL are returned to the screen.</p>
  
  <p>I am not interested in using a DOM or XML parsing library, since this is such a small application.</p>
  
  <p>How can I fix the regex so that only the value for <code>SRC</code> is updated?</p>
  
  <p>Thank you for your time!</p>