Getting domain name without TLD

前端 未结 4 1108
轻奢々
轻奢々 2020-12-21 07:12

I have this code right here:

    // get host name from URL
    preg_match(\'@^(?:http://)?([^/]+)@i\',
    \"http://www.joomla.subdomain.php.net/index.html\"         


        
4条回答
  •  悲哀的现实
    2020-12-21 07:19

    It's really easy:

    function get_tld($domain) {
        $domain=str_replace("http://","",$domain); //remove http://
        $domain=str_replace("www","",$domain); //remowe www
        $nd=explode(".",$domain);
        $domain_name=$nd[0];
        $tld=str_replace($domain_name.".","",$domain);
        return $tld;
    }
    

    To get the domain name, simply return $domain_name, it works only with top level domain. In the case of subdomains you will get the subdomain name.

提交回复
热议问题