Skip to content

Instantly share code, notes, and snippets.

Created May 4, 2015 22:56
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 anonymous/fe77c97e632675411c3c to your computer and use it in GitHub Desktop.
Save anonymous/fe77c97e632675411c3c to your computer and use it in GitHub Desktop.
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;
}
@mwordpress
Copy link

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment