Skip to content

Instantly share code, notes, and snippets.

@JMichaelRoach
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save JMichaelRoach/4edd8ec00584ad1defd9 to your computer and use it in GitHub Desktop.

Select an option

Save JMichaelRoach/4edd8ec00584ad1defd9 to your computer and use it in GitHub Desktop.
PHP function to get domain name (not full host or server name)
function get_domain_name() {
$server_name = $_SERVER["SERVER_NAME"];
$domain_name = explode('.', $server_name);
$i = count($domain_name) - 1;
$domain_name = implode('.',array($domain_name[$i - 1],$domain_name[$i]));
return $domain_name;
}
@JMichaelRoach
Copy link
Author

I've seen a lot of fancy regular expressions versions of this, but this seems to work fine with little effort, and is much more readable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment