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:

1. Replace 'https://' with: 'http://mydomain.com'.
2. Replace 'http://' with: 'http://mydomain.com'.
3. Replace 'www.' with: 'http://mydomain.com'.
4. 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!

Code:
<?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!