Skip to content

Instantly share code, notes, and snippets.

@sagive
Created January 20, 2015 01:47
Show Gist options
  • Save sagive/906aaa94141c090222a4 to your computer and use it in GitHub Desktop.
Save sagive/906aaa94141c090222a4 to your computer and use it in GitHub Desktop.
Clean url - return base url
/*******************************************
** 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