title: Regex to replace html src attribute in PHP
body: <p>I'm trying to use regex to replace source attribute (could be image or any tag) in PHP.</p>
  
  <p>I've a string like this:</p>
  
  <pre><code>$string2 = "&lt;html&gt;&lt;body&gt;&lt;img src = 'images/test.jpg' /&gt;&lt;img src = 'http://test.com/images/test3.jpg'/&gt;&lt;video controls="controls" src='../videos/movie.ogg'&gt;&lt;/video&gt;&lt;/body&gt;&lt;/html&gt;";
  </code></pre>
  
  <p>And I would like to turn it into:</p>
  
  <pre><code>$string2 = "&lt;html&gt;&lt;body&gt;&lt;img src = 'test.jpg' /&gt;&lt;img src = 'test3.jpg'/&gt;&lt;video controls="controls" src='movie.ogg'&gt;&lt;/video&gt;&lt;/body&gt;&lt;/html&gt;";
  </code></pre>
  
  <p>Heres what I tried :</p>
  
  <pre><code>$string2 = preg_replace("/src=["']([/])(.*)?["'] /", "'src=' . convert_url('$1') . ')'" , $string2);
  echo htmlentities ($string2);
  </code></pre>
  
  <p>Basically it didn't change anything and gave me a warning about unescaped string.</p>
  
  <p>Doesn't <code>$1</code> send the content of the string ? What is wrong here ?</p>
  
  <p>And the function of convert_url is from an example I posted here before : </p>
  
  <pre><code>function convert_url($url)
  {
      if (preg_match('#^https?://#', $url)) {
          $url = parse_url($url, PHP_URL_PATH);
      }
      return basename($url);
  }
  </code></pre>
  
  <p>It's supposed to strip out url paths and just return the filename.</p>