What is the best way to find if a string contains a url?
I'm writing a chatbox and need to deny the posting of urls in it...
if(preg_match('/[a-zA-Z]+:\/\/[0-9a-zA-Z;.\/?:@=_#&%~,+$]+/', $clean_message, $matches))
{
die('INVALID!');
}
seems to do the trick for urls that contain http:// but i need to also be able to deny urls such as youtube.com and if an ipaddress is posted etc..
I need to keep the chatbox clean from users spamming urls!
Probably the best you can do is something like:
/[\w\d\.]+\.(com|org|ca|net|uk)/
and add in all other common suffixes. Doing something like:
/[\w\d\.]+\.[\w\d]{1,3}/
might be too generic but would catch pretty much anything that looked like a URL.