I did this function a bit backwards. I would have a subdomain, but i wanted to get the base domain from the subdomain. I came up with this simple function :
function find_base_domain($url=false){
if($url== false){
$url = (@is_array($_SERVER)) ? $_SERVER['SERVER_NAME'] : ((@is_array($HTTP_SERVER_VARS)) ? $HTTP_SERVER_VARS['SERVER_NAME'] : $SERVER_NAME );
//If $_SERVER is an array, give the server name, if $HTTP_SERVER_VARS is an array, give the server name from that, if not, then just use the old old $SERVER_NAME variable;
}
//Continue with the rest of the function;
$url = str_replace("http://","",$url);
$domainExt = substr($url,strrpos($url,"."));
$domain = substr($url,0,strrpos($url,"."));
if(strstr($domain,".")){
$base_domain = substr($domain,strrpos($domain,".")+1);
}else{
$base_domain = $domain;
}
return $base_domain.$domainExt;
}
I plan on making it so it takes another argument, being $level, which says how many levels back you want to go(since some sites have subdomain1.subdomain2.domain.com and so on(like yahoo) ).






Print It