src/HTTP.php
public static function normaliseURL($url, array &$components = []){

		$url = filter_var(trim($url), FILTER_SANITIZE_URL);
		$components = @parse_url($url);

		if(!$components || !isset($components['host'])){

			return '';
		}

		// Scheme

		if(!preg_match('/https*/i', $components['scheme'])){

			switch($components['port']){

				case 443:

					$components['scheme'] = 'https';

				break;

				case 80:
				default:

					$components['scheme'] = 'http';

				break;
			}
		}

		// Remove default ports

		if(!empty($components['port'])){

			if($components['scheme'] == 'http' && $components['port'] == 80){

				unset($components['port']);
			}
			elseif($components['scheme'] == 'https' &&
				$components['port'] == 443){

				unset($components['port']);
			}
		}

		// Remove trailing dot from FQDN

		if(substr($components['host'], -1) == '.'){

			$components['host'] = substr($components['host'], 0, -1);
		}

		// Reconstruct URL

		$normalised =
			strtolower("{$components['scheme']}://{$components['host']}/");

		if(isset($components['port'])){

			$normalised = substr($normalised, 0, -1) . ":{$components['port']}/";
		}

		if(isset($components['path'])){

			// Clean path

			$components['path'] =
				str_replace(['\\', '/./'], '/', $components['path']);
			$components['path'] =
				preg_replace('/[\/]+/', '/', $components['path']);

			$normalised .= ltrim($components['path'], '/');
		}

		if(isset($components['query'])){

			$normalised .= "?{$components['query']}";
		}

		if(isset($components['fragment'])){

			$normalised .= "#{$components['fragment']}";
		}

		return $normalised;
	}
public static function normaliseURL($url, array &$components = []){

		$url = filter_var(trim($url), FILTER_SANITIZE_URL);
		$components = @parse_url($url);

		if(!$components || !isset($components['host'])){

			return '';
		}

		// Scheme

		if(!preg_match('/https*/i', $components['scheme'])){

			switch($components['port']){

				case 443:

					$components['scheme'] = 'https';

				break;

				case 80:
				default:

					$components['scheme'] = 'http';

				break;
			}
		}

		// Remove default ports

		if(!empty($components['port'])){

			if($components['scheme'] == 'http' && $components['port'] == 80){

				unset($components['port']);
			}
			elseif($components['scheme'] == 'https' &&
				$components['port'] == 443){

				unset($components['port']);
			}
		}

		// Remove trailing dot from FQDN

		if(substr($components['host'], -1) == '.'){

			$components['host'] = substr($components['host'], 0, -1);
		}

		// Reconstruct URL

		$normalised =
			strtolower("{$components['scheme']}://{$components['host']}/");

		if(isset($components['port'])){

			$normalised = substr($normalised, 0, -1) . ":{$components['port']}/";
		}

		if(isset($components['path'])){

			// Clean path

			$components['path'] =
				str_replace(['\\', '/./'], '/', $components['path']);
			$components['path'] =
				preg_replace('/[\/]+/', '/', $components['path']);

			$normalised .= ltrim($components['path'], '/');
		}

		if(isset($components['query'])){

			$normalised .= "?{$components['query']}";
		}

		if(isset($components['fragment'])){

			$normalised .= "#{$components['fragment']}";
		}

		return $normalised;
	}
public static function normaliseURL ( $ url , array & $ components = [ ] ) { $ url = filter_var ( trim ( $ url ) , FILTER_SANITIZE_URL ) ; $ components = @ parse_url ( $ url ) ; if ( ! $ components || ! isset ( $ components [ 'host' ] ) ) { return '' ; } // Scheme if ( ! preg_match ( '/https*/i' , $ components [ 'scheme' ] ) ) { switch ( $ components [ 'port' ] ) { case 443 : $ components [ 'scheme' ] = 'https' ; break ; case 80 : default : $ components [ 'scheme' ] = 'http' ; break ; } } // Remove default ports if ( ! empty ( $ components [ 'port' ] ) ) { if ( $ components [ 'scheme' ] == 'http' && $ components [ 'port' ] == 80 ) { unset ( $ components [ 'port' ] ) ; } elseif ( $ components [ 'scheme' ] == 'https' && $ components [ 'port' ] == 443 ) { unset ( $ components [ 'port' ] ) ; } } // Remove trailing dot from FQDN if ( substr ( $ components [ 'host' ] , - 1 ) == '.' ) { $ components [ 'host' ] = substr ( $ components [ 'host' ] , 0 , - 1 ) ; } // Reconstruct URL $ normalised = strtolower ( "{$components['scheme']}://{$components['host']}/" ) ; if ( isset ( $ components [ 'port' ] ) ) { $ normalised = substr ( $ normalised , 0 , - 1 ) . ":{$components['port']}/" ; } if ( isset ( $ components [ 'path' ] ) ) { // Clean path $ components [ 'path' ] = str_replace ( [ '\\' , '/./' ] , '/' , $ components [ 'path' ] ) ; $ components [ 'path' ] = preg_replace ( '/[\/]+/' , '/' , $ components [ 'path' ] ) ; $ normalised .= ltrim ( $ components [ 'path' ] , '/' ) ; } if ( isset ( $ components [ 'query' ] ) ) { $ normalised .= "?{$components['query']}" ; } if ( isset ( $ components [ 'fragment' ] ) ) { $ normalised .= "#{$components['fragment']}" ; } return $ normalised ; }
Normalise a URL.

<p>This function attempts to normalise a URL as best as possible. This
function requires any URL passed to at least contain a host name and
an indication of the scheme (HTTP or HTTPS) to be used.</p>

<p>The optional, pass-by-reference, parameter {@link $components} is set
to the result of the internal call to {@link parse_url()} done by this
method (with a little bit of post-processing applied). This information
can be used to, for example, retrieve the hostname from the URL.</p>

<ul>
<li>Invalid characters are removed from the URL;</li>
<li>If no scheme information is present, "http://" is prepended;</li>
<li>If the scheme and port match, the port is removed;</li>
<li>If present, the trailing dot is removed from the host name;</li>
<li>The hostname is made all lowercase;</li>
<li>Optional path, query and fragment are appended to the URL;</li>
<li>The path component is cleaned of unnecessary and incorrect
slashes.</li>
</ul>

@param string $url
@param array &$components
@return string
@see parse_url()
Normalise a URL .