0

I need a basic regular expression for validating a url, such that:

  • Both http:// and http://www. are allowed (and the same thing with https:// and https://www.).
  • All common tld like .com, .net. .org, .cc, .my, etc are allowed.
  • Things like http://site.com/dir/dir2/page.html etc are allowed
  • Only characters allowed in a url are allowed (such as alphanumeric chars, ?_-.& etc)

Basically something which will check if http:// or https:// is contained, and that at least one tld such as .com, .net, .[something] is contained.

Any ideas?

Ali
  • 261,656
  • 265
  • 575
  • 769

1 Answers1

7

Use php's filter

There are good examples on this page

var_dump( filter_var( 'http://example.com', FILTER_VALIDATE_URL ) );
Galen
  • 29,976
  • 9
  • 71
  • 89
  • Could you please turn this into a real answer so the core of the message is actually here. As it is now it's just a link answer and they're being edited or deleted these days. – markus Dec 09 '12 at 15:31