Last active
March 17, 2018 15:02
-
-
Save mjau-mjau/8a6395730c597f5e77007296f733d721 to your computer and use it in GitHub Desktop.
PHP get primary domain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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