// **************************************************************
// httphost -- Cut the hostname from protocol, port and uri and
// return information about host+sub, domain, tld.
//
// mixed httphost( string $url [, string $key ] )
function httphost ( $url, $key = Null ) {
(string) $hostname = $url;
// Cut the hostname from protocol, port and uri.
if ( preg_match( '/^(f|ht)tps?:\/\/([^\/:]+)/i', $url, $match ) ) {
$hostname = $match[2];
}
// Lambda function which remove the dot from host.
$clean = create_function( '$str', 'return preg_replace( "/\.$/", "", $str );' );
// Match the single elements from hostname (host+sub,domain,tld).
if ( preg_match( '/^([-a-z0-9_.]+\.)?([-a-z0-9_]+)\.([a-z]{2,})$/', $hostname, $host ) ) {
// Get the host without the trailing dot.
(string) $tmphost = $clean($host[1]);
// Check, if the host has sub-domains.
if ( preg_match( '/\./', $tmphost ) ) {
// Convert the host and sub-domain(s) into an array
(array) $subs = explode( '.', $tmphost );
// Get the hostname and remove them from array
(string) $name = array_shift( $subs );
// Store $hosr-array with host, sub, domain and tld.
(array) $hosts = array( 'host' => $name , 'sub' => $subs, 'domain' => $host[2], 'tld' => $host[3] );
} else {
// Store $hosr-array with host, domain and tld.
(array) $hosts = array( 'host' => $tmphost , 'domain' => $host[2], 'tld' => $host[3] );
}
}
// Return host information as assoc array
// or string, defined by key.
if ( isset($key) )
return $hosts[$key];
else
return $hosts;
}