PHP REGEX: Get domain from URL
I use:
$domain = parse_url('http://' . str_replace(array('https://', 'http://'), '', $url), PHP_URL_HOST);
Because parse_url
doesn't return host key when schema is missing in $url
.
There's no need to use a regex for this. PHP has an inbuilt function to do just this. Use parse_url()
:
$domain = parse_url($url, PHP_URL_HOST);