Created
May 4, 2015 22:56
-
-
Save anonymous/fe77c97e632675411c3c to your computer and use it in GitHub Desktop.
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
function getBaseDomain($domain = '') { | |
$result = false; | |
if($domain) { | |
$url = parse_url($domain); | |
if(isset($url['host']) && $url['host']) { | |
$parts = explode('.', $url['host']); | |
$middlePart = array_slice($parts, -2, 1)[0]; | |
$slice = ((strlen($middlePart) <= 3) && (count($parts) > 2)) ? 3 : 2; | |
$result = implode('.', array_slice($parts, (0 - $slice), $slice)); | |
} | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice job
make support for php 5.3+
$middlePart = array_slice($parts, -2, 1);
$mid = $middlePart[0];
$slice = ((strlen($mid) <= 3) && (count($parts) > 2)) ? 3 : 2;