Jun 15, 2022
6
Level 12
Best way to extract the MAIN DOMAIN from a URL
Hello everyone!
I'm looking for THE BEST & MOST RELIABLE way to extract the main domain name from URLS. Example:
https://www.123-456-789-012.cprapid.co.uk:2088/cpsess4243010201/frontend/jupiter/terminal/index.html
www.bob.bob.bob.bob.bob.co.uk/index.php?page=contacts
com.com/contact.php
https://www.sub.com.co.uk/index.php?domain=com.com
When passed it should return
cprapid.co.uk
bob.co.uk
com.com
com.co.uk
and so on...
I'm currently using
function get_domain($url) {
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : $pieces['path'];
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{0,63}\.[a-z\.]{1,5})$/i', $domain, $regs)) {
return $regs['domain'];
}
return false;
}
But it really fails sometimes.
I'm even ready to pay for a function that is 100% reliable.
Please or to participate in this conversation.