0

I'm trying to find a regular expression that is able to change all URLs of a curl'ed document from relative to absolute.

One of the way I found is the post here but it works only for the first URL and not for all.

This is the code I'm using:

$url="http://www.example.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);                                                   
curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, 0);                              
curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 60);                                
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);                                         
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);                                            
$result=curl_exec($ch);
curl_close($ch);
$result = preg_replace('~(href|src)=(["\'])(?!#)(?!http://)([^\2]*)\2~i','$1="http://www.example.com$3"', $result);
echo $result; 

Where am I doing wrong?

EDIT Just to explain better. I haven't an array of urls, but I have an entire document gathered from curl so I need a preg replace method.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Luca
  • 848
  • 1
  • 14
  • 33
  • Possible duplicate of [Transfrom relative path into absolute URL using PHP](http://stackoverflow.com/questions/4444475/transfrom-relative-path-into-absolute-url-using-php) – Ben Shoval Jul 23 '16 at 17:23
  • You could go this way with RamenChef's suggested modification, but will likely be happier with a more robust solution like the one at http://stackoverflow.com/questions/4444475/transfrom-relative-path-into-absolute-url-using-php. – Ben Shoval Jul 23 '16 at 17:24
  • Thank you, but that url works if I have an array of url. In this case I need a replace in a html document – Luca Jul 23 '16 at 18:11

1 Answers1

1

I'm not exactley sure why it replaces it just one time (maybe it has something to do with the backreference), but when you wrap it in a while loop, it should work.

$pattern = '~(href|src)=(["\'])(?!#|//|http)([^\2]*)\2~i';
while (preg_match($pattern, $result)) {
    $result = preg_replace($pattern,'$1="http://www.example.com$3"', $result);
}

(I also changed the pattern slightly.)

xystum
  • 939
  • 6
  • 8