Here's a really ugly way to do it... I'm sure there's a better one but this should work. Something like:
PHP Code:
$tld = array('com','net','org','co'); // might need to add more
$bits = explode('.',$host);
if($bits[0] == 'www')
array_shift($bits); // the www exists, but it's irrelevant so shift it off the start
if(count($bits) > 4)
{
//some crazy domain action with at least 2 subdomain levels, i'd give them the shaft... but you can reuse what's below for more subdomain levels if ya want to.
}
elseif(count($bits) == 4)
{
if(array_search($bits[2],$tld) !== false)
{
// third bit is a tld, so this is a domain.com.au with a subdomain
$domain = $bits[1] .'.'. $bits[2] .'.'. $bits[3];
}
else
{
//some crazy domain action with at least 2 subdomain levels or it's an ip, i'd give them the shaft
}
}
elseif(count($bits) == 2)
{
//can only have the name + tld
$domain = $bits[0] .'.'. $bits[1];
}
elseif(array_search($bits[1],$tld) !== false)
{
//there are three parts, second one is a tld, so this is a valid domain
$domain = $bits[0] .'.'. $bits[1] .'.'. $bits[2];
}
else
{
//there are three parts, but no tld as the second one, must be using a subdomain.
$domain = $bits[1] .'.'. $bits[2];
}
Hope that helps
