开发者

How do I detect subdomain and filter it?

I do have a domain search function. In search box you have the option to enter any kind of domain names. what I am looking into is how do I filter sub domain from search or else trim sub domain and keep only main.

for example if a user entered mail.yahoo.com then that 开发者_如何学Goto be convert to yahoo.com or it can be omitted from search.


Here's a more concise way to grab the domain and a likely subdomain from a URL.

function find_subdomain($url) {
    $parts = parse_url($url);
    $domain_parts = explode('.', $parts['host']);
    while(count($domain_parts) > 4)
        array_shift($domain_parts);
    return join('.', $domain_parts);
}

Keep in mind that not everything that looks like a subdomain is really a subdomain. Some countries have their own country-specific domains that everyone uses, like .co.uk and .com.au. You can not rely on the number of dots in the URL to tell you what is and is not a subdomain. In fact, you might need the opposite approach - first remove the top-level domain, then see what's left. Unfortunately then you're left with the second-level domain problem.

Can you tell us more about what exactly you are trying to accomplish? Why are you trying to detect subdomains? You mentioned a search box. What is being searched?


Edit: I have updated the function to up to four of the right-most parts of the domain. Given "http://one.two.three.four.five.six.com" it will return 'four.five.six.com'


I customized an utility function that i'm using, it's close to perfection (but that's what you could get without hard-coding all the possible list of domain extensions).

Here's the catch: the assumes that the main domain contains at least 4 characters. i.e for: sub.mail.com, it returns mail.com But for sub.aol.com it returns sub.aol.com

function get_main_domain($host='') {
    if(empty($host))$host=$_SERVER['HTTP_HOST'];
    $domain_parts = explode('.',$host);
    $count=count($domain_parts);
    if($count<=2)return $host;

    $permit=0;
    for($i=$count-1;$i>=0;$i--){
        $permit++;
        if(strlen($domain_parts[$i])>3)break;
    }

    while(count($domain_parts) >$permit)array_shift($domain_parts);
    return join('.', $domain_parts);
}


Well that doesnt work for all domain if you forgot to mention it in array...

Here is my solution...but I need to compress it to few lines...is it possible??

function subdomain($domainb){$bits = explode('/', $domainb);    
if ($bits[0]=='http:' || $bits[0]=='https:'){
$domainb= $bits[2];
} else {$domainb= $bits[0];}
unset($bits);
$bits = explode('.', $domainb); $idz=0;
while (isset($bits[$idz])){$idz+=1;}
$idz-=4; $idy=0;
while ($idy<$idz){ unset($bits[$idy]);
$idy+=1;} $part=array();
foreach ($bits AS $bit){$part[]=$bit;}
unset($bit); unset($bits); unset($domainb);
if (strlen($part[1])>4){ unset($part[0]);}
foreach($part AS $bit){$domainb.=$bit.'.';}
unset($bit);
return preg_replace('/(.*)\./','$1',$domainb);}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜