- rtrim: documentation ( source)
- parse_url: documentation ( source)
- strtolower: documentation ( source)
<?php
function cleanUrl($value)
{
$url = parse_url($value);
// Crap URLs (ie. without a scheme) only contain a path.
if (!isset($url['host'])) {
$url['host'] = rtrim($url['path'], '/');
$url['path'] = null;
}
$output = '';
$output .= !empty($url['scheme']) ? $url['scheme'] . '://' : 'http://'; // If unspecified, use HTTP.
$output .= strtolower($url['host']);
$output .= $url['path'];
$output .= !empty($url['query']) ? '?' . $url['query'] : null;
$output .= !empty($url['fragment']) ? '#' . $url['fragment'] : null;
return $output;
}
cleanUrl('http://google.com');