Created
November 18, 2012 07:02
-
-
Save n5i/4103935 to your computer and use it in GitHub Desktop.
Get domain url from link
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
// 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