Skip to content

Instantly share code, notes, and snippets.

@martinkhoury
Last active October 16, 2015 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinkhoury/dcaf0b7f2d76231d8c6d to your computer and use it in GitHub Desktop.
Save martinkhoury/dcaf0b7f2d76231d8c6d to your computer and use it in GitHub Desktop.
The following function will return the root domain given a URL.
<?php
/*
Get Root Domain from a URL with PHP
http://www.internoetics.com/2015/05/04/get-root-domain-from-a-url-with-php/
*/
function internoetics_get_domain($url) {
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $m)) {
return $m['domain'];
}
return false;
}
/* Usage: returns internoetics.com */
echo internoetics_get_domain("http://something.something.internoetics.com/directory/filename.php");
// returns internoetics.com
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment