Folks,
I’m afraid the following preg_replace is not doing the job. Can you think of a better one ?
Actually, if you can come-up with one that does the following then I’d appreciate it:
- Replace ‘https://’ with: ‘http://mydomain.com’.
- Replace ‘http://’ with: ‘http://mydomain.com’.
- Replace ‘www.’ with: ‘http://mydomain.com’.
- Replace all subdomains and sub sub domains etc. with: ‘http://mydomain.com’.
eg1. Replace ‘**mail.**domain.com’ with ‘http://mydomain.com’.
eg2. Replace ‘**ny.mail.**domain.com’ with ‘http://mydomain.com’.
eg3. Replace ‘**europe.spain.mail.**domain.com’ with ‘http://mydomain.com’.
eg4. Replace ‘**west.europe.deutchland.mail.**domain.com’ with ‘http://mydomain.com’.
And so on. You get the picture.
I tried the following code but it’s not working. Instead of replacing things with ‘http://mydomain.com’ it is replacing with: ‘%24url//’. And that is a big No! No!
<?php
$url = "http://google.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$url");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
$result = preg_replace("#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)([^\"'>]+)([\"'>]+)#",'$1http://$url/$2$3', $result);
echo $result
?>
Cheers!