Skip to content

Instantly share code, notes, and snippets.

@n5i
Created November 18, 2012 07:02
Show Gist options
  • Save n5i/4103935 to your computer and use it in GitHub Desktop.
Save n5i/4103935 to your computer and use it in GitHub Desktop.
Get domain url from link
// function extract a domain url from the link
// !Note: the domain name case is transformed to lowercase
function get_domain( $url ){
$url = strtolower( $url );
$base = str_ireplace( 'http://', '', $url );
$base = str_ireplace( 'https://', '', $base );
if( preg_match( '/^www\./i', $base ) ){
$base = str_ireplace( 'www.', '', $base );
$www = true;
}
$base_data = explode( "/", $base );
$domain = $base_data[0];
if( !empty( $www ) ){
$domain = 'www.'.$domain;
}
if( preg_match('/^http:\/\//i', $url ) ){
$base_url = 'http://'.$domain.'/';
}
if( preg_match('/^https:\/\//i', $url ) ){
$base_url = 'https://'.$domain.'/';
}
return $base_url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment