18.191.234.191 |    

Navigation

Google Advertisement

Cut the hostname from protocol, port and uri and return information about host+sub, domain, tld.

httphost
  1. // **************************************************************
  2. // httphost -- Cut the hostname from protocol, port and uri and
  3. // return information about host+sub, domain, tld.
  4. //
  5. //      mixed httphost( string $url [, string $key ] )
  6.  
  7.  
  8. function httphost ( $url, $key = Null ) {
  9.  
  10. (string) $hostname = $url;
  11.  
  12. // Cut the hostname from protocol, port and uri.
  13. if ( preg_match( '/^(f|ht)tps?:\/\/([^\/:]+)/i', $url, $match ) ) {
  14. 	$hostname = $match[2];
  15. }
  16.  
  17. // Lambda function which remove the dot from host.
  18. $clean = create_function( '$str', 'return preg_replace( "/\.$/", "", $str );' );
  19.  
  20. // Match the single elements from hostname (host+sub,domain,tld).
  21. if ( preg_match( '/^([-a-z0-9_.]+\.)?([-a-z0-9_]+)\.([a-z]{2,})$/', $hostname, $host ) ) {
  22.  
  23. 	// Get the host without the trailing dot.
  24. 	(string) $tmphost = $clean($host[1]);
  25.  
  26. 	// Check, if the host has sub-domains.
  27. 	if ( preg_match( '/\./', $tmphost ) ) {
  28. 		// Convert the host and sub-domain(s) into an array
  29. 		(array) $subs = explode( '.', $tmphost );
  30. 		// Get the hostname and remove them from array
  31. 		(string) $name = array_shift( $subs );
  32. 		// Store $hosr-array with host, sub, domain and tld.
  33. 		(array) $hosts = array( 'host' => $name , 'sub' => $subs, 'domain' => $host[2], 'tld' => $host[3] );
  34. 	} else {
  35. 		// Store $hosr-array with host, domain and tld.
  36. 		(array) $hosts = array( 'host' => $tmphost , 'domain' => $host[2], 'tld' => $host[3] );
  37. 	}
  38. }
  39.  
  40. // Return host information as assoc array
  41. // or string, defined by key.
  42. if ( isset($key) )
  43. 	return $hosts[$key];
  44. else
  45. 	return $hosts;
  46.  
  47. }
Parsed in 0.013 seconds at 117.76 KB/s

Search
 
Full text search by name and content of a snippet.

User online
There are 5 users online.

Tags Cloud

Latest snippets
str2seconds
(Bash::Function)
is_integer
(Bash::Function)
file_rotate
(Bash::Function)
confirm
(Bash::Function)
is_workingtime
(Bash::Function)
last day of last month
(Bash::Snippets)
crypt_apr1_md5
(PHP::Function)
crypt_apr1_md5
(Perl::Function)
transparent
(CSS)
rfc2822Toiso8601
(PHP::Function)