-
-
Save martinkhoury/dcaf0b7f2d76231d8c6d to your computer and use it in GitHub Desktop.
The following function will return the root domain given a URL.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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