title: preg_replace change link from href
body: <p>I need to replace urls in the page taken by curl and add correct link to images and links. My php curl code is: </p>
  
  <pre><code>&lt;?php
  
  $result = '&lt;a href="http://host.org"&gt;&lt;img src="./sec.png"&gt;&lt;/a&gt;
  &lt;link href="./styles.css" rel="alternate stylesheet" type="text/css" /&gt;
  &lt;script type="text/javascript" src="./style.js"&gt;&lt;/script&gt;';
  
  echo $result;
   if (!preg_match('/src="https?:\/\/"/', $result)) {
          $result = preg_replace('/src="(http:\/\/([^\/]+)\/)?([^"]+)"/', "src=\"http://google.com/\\3\"", $result);
      }
  echo $result;
  if (!preg_match('/href="https?:\/\/"/', $result)) {
          $result = preg_replace('/href="(http:\/\/([^\/]+)\/)?([^"]+)"/', "href=\"http://google.com/\\3\"", $result);
      }
  echo $result;
  
  ?&gt;
  </code></pre>
  
  <p>Output is:</p>
  
  <pre><code>//original links
  &lt;a href="http://host.org"&gt;&lt;img src="./sec.png"&gt;&lt;/a&gt;
  &lt;link href="./styles.css" type="text/css" /&gt;
  &lt;script src="./style.js"&gt;&lt;/script&gt;&lt;br /&gt;
  
  //fixed SRC path
  &lt;a href="http://host.org"&gt;&lt;img src="http://google.com/./sec.png"&gt;&lt;/a&gt;
  &lt;link href="./styles.css" type="text/css" /&gt;
  &lt;script src="http://google.com/./style.js"&gt;&lt;/script&gt;
  
  //fixed HREF path
  &lt;a href="http://google.com//google.com/./sec.png"&gt;&lt;/a&gt;
  &lt;link href="http://google.com/./styles.css" type="text/css" /&gt;
  &lt;script src="http://google.com/./style.js"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>But when the link is "a" it cut all link and left only href value. </p>
  
  <pre><code>//from
  &lt;a href="http://host.org"&gt;&lt;img src="./sec.png"&gt;&lt;/a&gt;
  //to src fix:
  &lt;a href="http://host.org"&gt;&lt;img src="http://google.com/./sec.png"&gt;&lt;/a&gt;
  //ERRRROR when href fix make :
  &lt;a href="http://google.com//google.com/.sec.png"&gt;&lt;/a&gt;
  </code></pre>
  
  <p>Can any body help in fix it. Thank you</p>