Find out the subdomain part of a domain

Use this code to find the subdomain part of a domain.

function SubdomainGet ($domain) {
    // Domain has to be formatted WITHOUT the "http://" part
    // eg.
    //     $domain = 'mydomain.com';

    $server_address = $_SERVER['HTTP_HOST'];

    // Strip the domain and leave the subdomain
    $sub_domain = str_replace("." . $domain, "", $server_address);

    if ($sub_domain == $server_address) {
    $sub_domain = '';
}

return $sub_domain;
}

You could use it like this:

$subdomain = SubdomainGet('example.com');
Leave a comment
Name
Email (optional)
Your email will only be used to show your Gravatar and to send you replies to this article (if you opt to receive them). You won't get ANY other messages at all!
Comment
No HTML allowed
Notify me via email
 
 
Share

Valid HTML 4.01 Strict