9

I've gone through all possible ways for having the regular expression for the url validation, but I didn't get any.. what I need is the url can be like

google.com
http://google.com
https://google.com
http://www.google.com
https://www.google.com

but it should not allow if it is just google

finally the thing is the .extensions are mandatory

I've tried this /^[a-z0-9-]+(.[a-z0-9-]*)(.[a-z0-9-]*)$/

can anyone help me in this case..

kumar
  • 1,796
  • 2
  • 15
  • 37

5 Answers5

8

You can directly validate url using filter_var and FILTER_VALIDATE_URL

if (filter_var($url, FILTER_VALIDATE_URL) !== false)

Edit

With Regex

$subject = "http://www.google.com";
$pattern = "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i";
preg_match($pattern, $subject, $matches);
print_r($matches);

Output

Array ( [0] => http://www.google.com )

Codepad

Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
  • I'm validating with the regular expression which will be stored in my database, I cannot use these kind of conditions.. – kumar May 08 '13 at 06:32
  • Why not, use it before inserting in database. – Yogesh Suthar May 08 '13 at 06:34
  • @Nagendra see edited answer for regular expression. – Yogesh Suthar May 08 '13 at 06:43
  • Nice one, but it won't validate some protocols, for example if you want to validate stream URLs you should change this part `(?:https?|ftp)` to this `(?:https?|ftp|rtmp|rtsp|mms)` is possible I'm missing others – aesede Mar 10 '14 at 19:50
5

Try this Expression

/[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi

It will aceept all the cases that you have mentioned above

Hareesh
  • 519
  • 2
  • 15
  • I'm not familiar with regular expression. Would you please give me the php code? – Rikas Jul 09 '15 at 12:33
  • 1
    Been using this regex for a while and ran into a .financial site.... so the {2,4} for the top level domain should be {2,9} – Tyler Apr 28 '16 at 16:57
1
((https?:|[^.])\/\/w{0,3}[.]?google.[a-z]{2,4})|google.[a-z]{2,4}

It is not the prettiest but it works. I've checked with http://regexpal.com/

Robert
  • 19,800
  • 5
  • 55
  • 85
  • 1
    Let me propose several improvements. **1** you could use `https?` this will match both http or https, **2** you don't need to put the `w` in a character class, **3** `{0,1}` means `?` so you can make it `\.?`. Although this regex just works for this example, what if I had `http://translate.google.com` or `http://google.fr` ? – HamZa May 08 '13 at 06:50
  • 1
    Yes you're right about some improvements. He wanted it for examples he gave so I wrote it for that examples. If he wants domain It can be easily added. I've answered what he wanted or what I thought he wanted :) – Robert May 08 '13 at 06:55
0

You can try the following

(^(http|https)://)?(www\.)?\w+\.com
Jaydeep Rajput
  • 3,605
  • 17
  • 35
0

If you want to create really good domain validator you should get database of domain tlds and make validation for them.

Mozilla has that list: http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1

Here is a python module for that https://github.com/nmmmnu/TLDExtractor

nacholibre
  • 3,874
  • 3
  • 32
  • 35