0

I know this question has been asked many times before but none of the options seem to work for me (including the regexes and php's filter_var). I'm looking for a way to make sure a URL is a valid possibility before continuing, below are examples of valid URLs

google.com
www.google.com
http://www.google.com
https://www.google.com
http://google.com
https://google.com
domain-site.com
google.net
google.ca
google.email
IPs (ex. 198.XX.XXX.XXX)

Here are invalid URLs

test
http://test
https://test

Below are links/things I've tried to get the result I want above:

PHP regex for url validation, filter_var is too permisive

filter_var($url_new, FILTER_VALIDATE_URL)
preg_match('#^((https?)://(?:([a-z0-9-.]+:[a-z0-9-.]+)@)?([a-z0-9-.]+)(?::([0-9]+))?)(?:/|$)((?:[^?/]*/)*)([^?]*)(?:\?([^\#]*))?(?:\#.*)?$#i', $toLoad, $tmp)

etc

I know I can cURL to check if a domain exists and this may be the easiest option but its also very slow, I would prefer a regex type solution where I can get a preliminary check of the domain's validity.

Any help is appreciated!

Community
  • 1
  • 1
Pharm
  • 180
  • 1
  • 11
  • Why would you validate the TLD (the extension as you call it) for 6 characters when there are TLD's which are longer i.e. `.finance`? – naththedeveloper Apr 22 '14 at 12:19
  • 1
    I can't understand why `google.com` (that isn't a valid URL) should pass validation and `http://test` (that is a valid URL) shouldn't. Are you by chance talking about active domain names or reachable web sites? – Álvaro González Apr 22 '14 at 12:19
  • I didn't realize TLDs were greater than 6 characters, my mistake. As for http://test vs google.com, I am not looking for valid markup. I can add the http:// to the domain if its not already there, my concern is validating if the domain is even a possibility (so yes active domain names that anyone could access using a browser). – Pharm Apr 22 '14 at 12:21

0 Answers0