tags:

views:

78

answers:

2

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 to be convert to yahoo.com or it can be omitted from search.

How can be possible with PHP?? or any ??

A: 

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);}
mathew
A: 

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'

Charles
As I mentioned earlier this is a domain search application like domaintools.com. so who ever searches in search box there may be chance of checking subdomains. this application does not provide any info on subdomains and most of the info is lies with top-level. and regarding country level yes off course I need that and my function work well with that too. chances of entering more than 4 dots sub domains are very rare in this case(db.main.cdn.google.com). so my filter only works upto this and I am ok with it. so my question is how do we compress it to few lines??
mathew
I have updated my code example to return only the four right-most parts of the domain. It's quite a bit more concise than your existing code.
Charles
I will give better explanation :domain name+tld or domain name+sld that's it. in this case "six.com"
mathew
Change the single `4` in my example to `2`.
Charles
No if it is google.co.uk then it will result -> co.uk
mathew
As I said above: "You *can not* rely on the number of dots in the URL to tell you what is and is not a subdomain."
Charles