$1
isn't a real variable, so you can't use str_replace
on it.
Try this :
$site = preg_replace('/<a href="http:\/\/(.*?)">/','<a href="mypage.php?url=$1">', $site);
By just putting the http:// in the pattern you ensure that it won't be in $1
. Of course you do need to escape the /
by putting a backslash in front of it. Hence the :\/\/
If you need this to work for https as well, try :
$site = preg_replace('/<a href="https?:\/\/(.*?)">/','<a href="mypage.php?url=$1">', $site);
The ?
after the s
will allow it to either be there or not. Of course this will rewrite the link https://google.com
to mypage.php?url=google.com
which may not be what you want (you no longer know whether it was http or https)
For the www. part :
$site = preg_replace('/<a href="https?:\/\/(www\.|)(.*?)">/','<a href="mypage.php?url=$2">', $site);