Extract the host, domain and subdomain name from a fully-qualified host name, assumed to be in $sbFQDN. For an application (from which this came from) see how to Reduce email address to its minimum expression.
# Assuming $sbFQDN is "host.subdomain.domain.edu", then # $sbHost = host.subdomain.domain.edu # $sbSubdomain = subdomain.domain.edu # $sbDomain = domain.edu # $sub = subdomain # $host = host # Assuming $sbFQDN is "host.domain.edu", then # $sbHost = host.domain.edu # $sbSubdomain = domain.edu # $sbDomain = domain.edu # $sub = "" # $host = host ($sbHost, $sbSubdomain, $sbDomain)= ($sbFQDN=~/^([^.]+\.((?:[^.]+\.)?([^.]+\.[^.]+)))$/); die "Could not obtain the local domain from hostname '$sbFQDN'\n" unless $sbDomain; $sbSubdomain=$sbDomain unless $sbSubdomain; ($sub=$sbSubdomain)=~s/\.?$sbDomain$//; ($host=$sbHost)=~s/\.?$sbSubdomain//;

Replies are listed 'Best First'.
RE: Getting hostname, domainname and subdomainname
by TQuid (Sexton) on Jun 27, 2000 at 06:50 UTC
    If anyone has any idea how to extract the subdomain &c. from a non-international TLD, I'd sure like to know it. Near as I can tell, puzzling out something as "simple" as
    very.fractured.vancouver.bc.ca
    is impossible to puzzle out without a database of all TLD's and all of their provincial & municipal subdomains. --TQuid
      Every zone is free to divide itself up any way it sees fit. Different TLD's use different strategies. Some are broken down geographically (Canada does it this way); some are broken down by type (the UK uses this method: commercial, educational, etc.); some are not broken down by any regular scheme (Japan). Aditionally, the domain being subdomained may (or may not) place any arbitrary order on the subdivision. These restrictions are purely arbitrary and have no relation to the DNS process.