Created
January 20, 2015 01:47
-
-
Save sagive/906aaa94141c090222a4 to your computer and use it in GitHub Desktop.
Clean url - return base 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
/******************************************* | |
** FUNCTION TO GET BASE URL | |
*******************************************/ | |
function normalize_url($url) { | |
$parts = parse_url($url); | |
$countParts = count(split('[.]', $parts['host'])); | |
if (substr($parts['host'], 0, 4) == 'www.') { | |
$base_url = str_replace('www.', '', $parts['host']); | |
} | |
elseif($countParts > 2) { | |
$base_url = ''; | |
$url_parts = split('[.]', $parts['host']); | |
array_shift($url_parts); | |
$counter = 0; | |
$arr_count = count($url_parts); | |
foreach($url_parts as $part) {$base_url .= $part.'.';} | |
$base_url = substr($base_url, 0, -1); | |
} | |
else{ | |
$base_url = $parts['host']; | |
} | |
return $base_url; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment