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?

1

1 Answer 1

7

Use php's filter

There are good examples on this page

var_dump( filter_var( 'http://example.com', FILTER_VALIDATE_URL ) );
Sign up to request clarification or add additional context in comments.

1 Comment

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.

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.