Skip to content

Instantly share code, notes, and snippets.

@mjau-mjau
Last active March 17, 2018 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjau-mjau/8a6395730c597f5e77007296f733d721 to your computer and use it in GitHub Desktop.
Save mjau-mjau/8a6395730c597f5e77007296f733d721 to your computer and use it in GitHub Desktop.
PHP get primary domain
<?php
function get_domain($host){
$myhost = strtolower(trim($host));
$count = substr_count($myhost, '.');
// www.(domain.com) || (domain.co.uk) || sub.(domain.com)
if($count === 2){
if(strlen(explode('.', $myhost)[1]) > 3) $myhost = explode('.', $myhost, 2)[1];
// re-run the function after removing first subdomain segment | www.sub.(domain.com) or www.(domain.co.uk) or www.sub.sub.sub.(domain.co.uk)
} else if($count > 2){
$myhost = get_domain(explode('.', $myhost, 2)[1]);
}
return $myhost;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment