Skip to content

Instantly share code, notes, and snippets.

@mertenvg
Created July 31, 2013 07:34
Show Gist options
  • Save mertenvg/6120059 to your computer and use it in GitHub Desktop.
Save mertenvg/6120059 to your computer and use it in GitHub Desktop.
Trim a domain to a specific number of levels
/**
* Trim a domain to a specific number of levels
*
* @param string $domain
* @param int $levels
*
* @return string
*/
function trim_domain($domain, $levels = 2)
{
if (empty($domain)) {
return '';
}
return implode('.', array_slice(explode('.', $domain), -($levels)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment