2

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!

7
  • 2
    Why bother? You'll never stop stuff like www [dot] youtube [dot] com. Filter for automated spam postings, and let it go? Commented Nov 24, 2011 at 1:10
  • I can spam a URL like this: s t a c k o v e r f l o w . c o m Commented Nov 24, 2011 at 1:11
  • You can do like Office Communicator and just add _ to known web protocols. Like: _http://www.example.com. Then it's up to the person on the other end to follow through. Commented Nov 24, 2011 at 1:12
  • @Brad, str_replace("[dot]", ".", $url); Commented Nov 24, 2011 at 1:15
  • 3
    @E3pO: Whatever rule you come up with, I can come up with a way to circumvent it. Commented Nov 24, 2011 at 1:16

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

That works great now but we run into the issue of people saying things such as "wow.that.is.cool!"
wow.that.is.cool could be a url, I don't think there's any way to know that it's not.
You're going to have to decide where to draw the line between letting some urls through, and not letting users enter something they want that isn't a url. You're going to end up with a bit in each anyway you do it.
Actually I just realized there's another issue, this would block "100.00". You should probably replace the [\w\d\.] with something like [\w\d\.]*\w[\w\d\.]*

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.