I need to validate domain names using PHP.
Only this format should be allowed: google.com
, subdomain.stack-overflow.org
etc
It should only allow - (in case of special characters)
No other format should be allowed ex: http, https,?,/index.php,www.
etc (none of these should be allowed).
Also i do not want to allow: localhost
,mydomain.com
I have tried: FILTER_VALIDATE_URL
but it accepts http
etc.
I also tried something regular expression like this: [a-z0-9-]+(.[a-z0-9-]+)
But it is allowing ex: stackoverflow
(without a .org
).
How can I achieve that?
$urlparts = parse_url(filter_var($url, FILTER_SANITIZE_URL));
if(!isset($urlparts['host'])){
$urlparts['host'] = $urlparts['path'];
}