2

I've been trying to find a solution myself but I couldnt do it so far (even tried all regexlib.com suggestions), so:

Need a regex that matches URLs like (PHP code):

http://example.com orhttp://www.example.com or www.example.com example.com


I've been trying to use this one (catch http://(optional)www.example.com/path/otherpath):

$regex = "/(((ftp|https?)://www.)|(www.)|(http://))([\d\w-.]+?)+(:\d+)?(/([\w/,=]*(\?\S+)?)?)?/";

but it's not enough.

Still missing the example.com. Any suggestion?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Davi Bandeira
  • 116
  • 1
  • 6
  • 1
    I suggest searching google and StackOverflow. This is a very broad and complex topic which has been discussed several times. – mike Aug 09 '10 at 13:56
  • Sorry, what exactly do you want to do - match *only* urls that include either the `http://` and (optionally?) the `www.`, or are you just looking to match all valid urls? (i.e. `http://www.stackoverflow.com`, `http://stackoverflow.com`, `www.stackoverflow.com`, and `stackoverflow.com`) – Stephen Aug 09 '10 at 13:58
  • http://stackoverflow.com/questions/206059/php-validation-regex-for-url – Ivan Krechetov Aug 09 '10 at 13:59

1 Answers1

4

How about this?

(www|http:\/\/|https:\/\/)?([\.[:alnum:]_-]){0,4}([[:alnum:]_-]+\.)([[:alnum:]_-]\.?)([[:alpha:]]){0,3}
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0x90
  • 293
  • 2
  • 9