We are The Media Dudes. Take a look at our collective blog and find out what we’ve been making, where we’ve been going and what we’ve been eating...

How To Remove URLs In PHP

Posted by Anthony on Friday August 14th 2009 at 15:30.

I spent a while looking for something that would remove URLs from some text. The only thing I managed to find was one that would remove only complete URLs e.g. http://www.themediadudes.com. As nobody writes like that when spamming and this was going to be used on something that anyone can write in, I needed something better.

I made my own thing and I'm sharing it with you so you dont have to search, find nothing, then make your own like I did.

$text = preg_replace("/(^|\s)(http:\/\/)?(www\.)?[0-9a-zA-Z\-_~]+\.(com|net|org|info|name|biz|.+\.\w\w)((\/)?[0-9a-zA-Z\.\-_~#]+)?\b/ie","",$text);

My regular expression skills are teh sux, so you can probably tweak that a lot.