Skip to content

Instantly share code, notes, and snippets.

@JeremyMorgan
Created June 10, 2013 20:08
Show Gist options
  • Save JeremyMorgan/5751828 to your computer and use it in GitHub Desktop.
Save JeremyMorgan/5751828 to your computer and use it in GitHub Desktop.
PHP Function to return a friendly domain name from a URL
/**
* 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