Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete subdomain from url string if subdomain is found

I have an array of domains like this:

domain.com
second.com
www.third.com
www.fourth.fifth.com
sixth.com
seventh.eigth.com

what I want is a function to return me the host only. Without subdomain.

This code is what i have so far for getting the hostname:

$parse = parse_url($url);
$domain = $parse['host'];

But this returns this only:

domain.com
second.com
third.com
fourth.fifth.com
sixth.com
seventh.eigth.com

I would need this output though:

domain.com
second.com
third.com
fifth.com
sixth.com
eigth.com
like image 733
ItsMeDom Avatar asked Feb 16 '14 09:02

ItsMeDom


People also ask

How do you decommission a subdomain?

Click on subdomain, on the list of all your subdomains, look for the one you wish to delete, then delete it from there.

Can a URL have multiple subdomains?

Each domain name can have up to 500 subdomains. You can also add multiple levels of subdomains, such as info.blog.yoursite.com. A subdomain can be up to 255 characters long, but if you have multiple levels in your subdomain, each level can only be 63 characters long.


2 Answers

function giveHost($host_with_subdomain) {
    $array = explode(".", $host_with_subdomain);

    return (array_key_exists(count($array) - 2, $array) ? $array[count($array) - 2] : "").".".$array[count($array) - 1];
}
like image 192
Baptiste Donaux Avatar answered Nov 15 '22 22:11

Baptiste Donaux


Try this code

 <?php
    /**
    * @param string $domain Pass $_SERVER['SERVER_NAME'] here
    * @param bool $debug
    *
    * @debug bool $debug
    * @return string
    */
    function get_domain($domain, $debug = false) {
        $original = $domain = strtolower($domain);     
        if (filter_var($domain, FILTER_VALIDATE_IP)) { return $domain; }    

        $debug ? print('<strong style="color:green">&raquo;</strong> Parsing: '.$original) : false; //DEBUG 

        $arr = array_slice(array_filter(explode('.', $domain, 4), function($value){
                            return $value !== 'www'; }), 0); //rebuild array indexes

        if (count($arr) > 2)    {
            $count = count($arr);
            $_sub = explode('.', $count === 4 ? $arr[3] : $arr[2]);

            $debug ? print(" (parts count: {$count})") : false; //DEBUG

            if (count($_sub) === 2)  { // two level TLD
                $removed = array_shift($arr);
                if ($count === 4) // got a subdomain acting as a domain
                    $removed = array_shift($arr);            
                $debug ? print("<br>\n" . '[*] Two level TLD: <strong>' . join('.', $_sub) . '</strong> ') : false; //DEBUG
            }
            elseif (count($_sub) === 1){ // one level TLD
                $removed = array_shift($arr); //remove the subdomain             
                if (strlen($_sub[0]) === 2 && $count === 3) // TLD domain must be 2 letters
                    array_unshift($arr, $removed);                
                else{
                    // non country TLD according to IANA
                    $tlds = array(    'aero',    'arpa',    'asia',    'biz',    'cat',    'com',    'coop',    'edu',    'gov',    'info',    'jobs',    'mil',    'mobi',    'museum',    'name',    'net',    'org',    'post',    'pro',    'tel',    'travel',    'xxx',    );             
                    if (count($arr) > 2 && in_array($_sub[0], $tlds) !== false) {//special TLD don't have a country
                        array_shift($arr);
                    }
                }
                $debug ? print("<br>\n" .'[*] One level TLD: <strong>'.join('.', $_sub).'</strong> ') : false; //DEBUG
            }
            else { // more than 3 levels, something is wrong
                for ($i = count($_sub); $i > 1; $i--) 
                    $removed = array_shift($arr);

                $debug ? print("<br>\n" . '[*] Three level TLD: <strong>' . join('.', $_sub) . '</strong> ') : false; //DEBUG
            }
        }
        elseif (count($arr) === 2) {
            $arr0 = array_shift($arr);     
            if (strpos(join('.', $arr), '.') === false
                        && in_array($arr[0], array('localhost','test','invalid')) === false) // not a reserved domain
            {
                $debug ? print("<br>\n" .'Seems invalid domain: <strong>'.join('.', $arr).'</strong> re-adding: <strong>'.$arr0.'</strong> ') : false; //DEBUG
                // seems invalid domain, restore it
                array_unshift($arr, $arr0);
            }
        }     

        $debug ? print("<br>\n".'<strong style="color:gray">&laquo;</strong> Done parsing: <span style="color:red">' . $original . '</span> as <span style="color:blue">'. join('.', $arr) ."</span><br>\n") : false; //DEBUG     
        return join('.', $arr);
    }


     //TEST
    $urls = array(
    'www.example.com' => 'example.com',
    'example.com' => 'example.com',
    'example.com.br' => 'example.com.br',
    'www.example.com.br' => 'example.com.br',
    'www.example.gov.br' => 'example.gov.br',
    'localhost' => 'localhost',
    'www.localhost' => 'localhost',
    'subdomain.localhost' => 'localhost',
    'www.subdomain.example.com' => 'example.com',
    'subdomain.example.com' => 'example.com',
    'subdomain.example.com.br' => 'example.com.br',
    'www.subdomain.example.com.br' => 'example.com.br',
    'www.subdomain.example.biz.br' => 'example.biz.br',
    'subdomain.example.biz.br' => 'example.biz.br',
    'subdomain.example.net' => 'example.net',
    'www.subdomain.example.net' => 'example.net',
    'www.subdomain.example.co.kr' => 'example.co.kr',
    'subdomain.example.co.kr' => 'example.co.kr',
    'example.co.kr' => 'example.co.kr',
    'example.jobs' => 'example.jobs',
    'www.example.jobs' => 'example.jobs',
    'subdomain.example.jobs' => 'example.jobs',
    'insane.subdomain.example.jobs' => 'example.jobs',
    'insane.subdomain.example.com.br' => 'example.com.br',
    'www.doubleinsane.subdomain.example.com.br' => 'example.com.br',
    'www.subdomain.example.jobs' => 'example.jobs',
    'test' => 'test',
    'www.test' => 'test',
    'subdomain.test' => 'test',
    'www.detran.sp.gov.br' => 'sp.gov.br',
    'www.mp.sp.gov.br' => 'sp.gov.br',
    'ny.library.museum' => 'library.museum',
    'www.ny.library.museum' => 'library.museum',
    'ny.ny.library.museum' => 'library.museum',
    'www.library.museum' => 'library.museum',
    'info.abril.com.br' => 'abril.com.br',
    '127.0.0.1' => '127.0.0.1',
    '::1' => '::1',
    );

    $failed = 0;
    $total = count($urls);

    foreach ($urls as $from => $expected){
        $from = get_domain($from, true);
        if ($from !== $expected){
            $failed++;
            print("<div style='color:fuchsia;'>expected {$from} to be {$expected}</div>");
        }
    }    
    if ($failed)    
        print("{$failed} tests failed out of {$total}");    
    else    
        print("Success");   

all credit goes to pocesar

like image 22
Sanoob Avatar answered Nov 15 '22 23:11

Sanoob