Parsing domain from a URL

前端 未结 18 2478
独厮守ぢ
独厮守ぢ 2020-11-22 12:26

I need to build a function which parses the domain from a URL.

So, with

http://google.com/dhasjkdas/sadsdds/sdda/sdads.html

or

18条回答
  •  半阙折子戏
    2020-11-22 12:53

    Please consider replacring the accepted solution with the following:

    parse_url() will always include any sub-domain(s), so this function doesn't parse domain names very well. Here are some examples:

    $url = 'http://www.google.com/dhasjkdas/sadsdds/sdda/sdads.html';
    $parse = parse_url($url);
    echo $parse['host']; // prints 'www.google.com'
    
    echo parse_url('https://subdomain.example.com/foo/bar', PHP_URL_HOST);
    // Output: subdomain.example.com
    
    echo parse_url('https://subdomain.example.co.uk/foo/bar', PHP_URL_HOST);
    // Output: subdomain.example.co.uk
    

    Instead, you may consider this pragmatic solution. It will cover many, but not all domain names -- for instance, lower-level domains such as 'sos.state.oh.us' are not covered.

    function getDomain($url) {
        $host = parse_url($url, PHP_URL_HOST);
    
        if(filter_var($host,FILTER_VALIDATE_IP)) {
            // IP address returned as domain
            return $host; //* or replace with null if you don't want an IP back
        }
    
        $domain_array = explode(".", str_replace('www.', '', $host));
        $count = count($domain_array);
        if( $count>=3 && strlen($domain_array[$count-2])==2 ) {
            // SLD (example.co.uk)
            return implode('.', array_splice($domain_array, $count-3,3));
        } else if( $count>=2 ) {
            // TLD (example.com)
            return implode('.', array_splice($domain_array, $count-2,2));
        }
    }
    
    // Your domains
        echo getDomain('http://google.com/dhasjkdas/sadsdds/sdda/sdads.html'); // google.com
        echo getDomain('http://www.google.com/dhasjkdas/sadsdds/sdda/sdads.html'); // google.com
        echo getDomain('http://google.co.uk/dhasjkdas/sadsdds/sdda/sdads.html'); // google.co.uk
    
    // TLD
        echo getDomain('https://shop.example.com'); // example.com
        echo getDomain('https://foo.bar.example.com'); // example.com
        echo getDomain('https://www.example.com'); // example.com
        echo getDomain('https://example.com'); // example.com
    
    // SLD
        echo getDomain('https://more.news.bbc.co.uk'); // bbc.co.uk
        echo getDomain('https://www.bbc.co.uk'); // bbc.co.uk
        echo getDomain('https://bbc.co.uk'); // bbc.co.uk
    
    // IP
        echo getDomain('https://1.2.3.45');  // 1.2.3.45
    

    Finally, Jeremy Kendall's PHP Domain Parser allows you to parse the domain name from a url. League URI Hostname Parser will also do the job.

提交回复
热议问题