How to get the first subdomain with PHP?

前端 未结 5 838
广开言路
广开言路 2020-12-11 23:10

I have a static domain of dev.example.com with wildcard subdomains like so *.dev.example.com.

I need to detect the name of the current wild

5条回答
  •  死守一世寂寞
    2020-12-11 23:34

    I think using parse_url function is much better approach:

    getUrlSubdomain($url){
        $urlSegments = parse_url($url);
        $urlHostSegments = explode('.', $urlSegments['host']);
    
        if(count($urlHostSegments) > 2) {
            return $urlHostSegments[0];
        }
        else{
            return null;
        }
    }
    

提交回复
热议问题