Recently I encountered a problem
Is the need to do some switching operation in accordance with the subdomain URL
I have seen articles in This
So I put it sort out
For your reference
//Added a function to obtain a subdomain
function extract_subdomains($domain)
    $subdomains = $domain;
    //Use PREG to get URL HOST
    if(preg_match("/([a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i",$subdomains, $matches))
        $domain = $matches[1];
        $domain = $domain;
    //get before HOST
    $subdomains = strstr($subdomains, $domain, true);
    //get before first  "." 
    $subdomains = rtrim($subdomains,'.');
    return $subdomains;

echo extract_subdomains("sub1.sub2.sub3.example.com.tw");
Hope it's helpful!