Created
June 10, 2013 20:08
-
-
Save JeremyMorgan/5751828 to your computer and use it in GitHub Desktop.
PHP Function to return a friendly domain name from a URL
This file contains hidden or 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
/** | |
* Function to return friendly domain name (example.com) from a full URL | |
* @param string $url | |
* @return string | |
*/ | |
function getDomain($url){ | |
// remove the protocol | |
$url = str_replace((array("http://", "https://")), "", $url); | |
// break up url by slashes | |
$urlary = explode("/", $url); | |
// count array elements | |
$domainary = explode(".", $urlary[0]); | |
// return last two elements (domain and TLD) | |
return $domainary[(count($domainary) - 2)] . "." . $domainary[(count($domainary) - 1)]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment