Question

I spent a few hours trying to figure this out and I just cannot get it. I simply want the preg_replace to only remove the http:// out of an anchor tag. I tried this str_replace but as you already know it doesn't work inside a preg_replace:

$site = preg_replace('/<a href="(.*?)">/','<a href="mypage.php?url='. str_replace("http://", "", "$1") .'">', $site);

I want only want the any anchor link to remove the http:// part out, but not for image links so it wont break < img src="....." > tags. Only urls for anchor tags. Actually if you could make it remove http:// and the www. part out too that would be great. So http://google.com or http://www.google.com will appear as google.com. Thanks.

Was it helpful?

Solution

$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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top