title: file_get_contents( - Fix relative urls
body: <p>I am trying to display a website to a user, having downloaded it using php. 
  This is the script I am using:</p>
  
  <pre><code>&lt;?php
  $url = 'http://stackoverflow.com/pagecalledjohn.php';
  //Download page
  $site = file_get_contents($url);
  //Fix relative URLs
  $site = str_replace('src="','src="' . $url,$site);
  $site = str_replace('url(','url(' . $url,$site);
  //Display to user
  echo $site;
  ?&gt;
  </code></pre>
  
  <p>So far this script works a treat except for a few major problems with the str_replace function. The problem comes with relative urls. If we use an image on our made up pagecalledjohn.php of a cat (Something like this: <img src="https://i.stack.imgur.com/8hJby.jpg" alt="Cat">). It is a png and as I see it it can be placed on the page using 6 different urls:</p>
  
  <pre><code>1. src="//www.stackoverflow.com/cat.png"
  2. src="http://www.stackoverflow.com/cat.png"
  3. src="https://www.stackoverflow.com/cat.png"
  4. src="somedirectory/cat.png" 
  </code></pre>
  
  <p><em>4 is not applicable in this case but added anyway!</em></p>
  
  <pre><code>5. src="/cat.png"
  6. src="cat.png"
  </code></pre>
  
  <p>Is there a way, using php, I can search for src=" and replace it with the url (filename removed) of the page being downloaded, but without sticking url in there if it is options 1,2 or 3 and change procedure slightly for 4,5 and 6?</p>