src/HTTP.php
public static function getBaseURL($url){

		$parsed_url = parse_url($url);
		if($parsed_url === false){

			throw new ErrorException('Malformed URL.');
		}

		$base_url = [];

		if(empty($parsed_url['scheme'])){

			throw new ErrorException('Malformed URL, no scheme.');
		}

		$base_url[] = $parsed_url['scheme'];
		$base_url[] = '://';
		$base_url[] = $parsed_url['host'];

		// Check the path

		$path = $parsed_url['path'];
		$path_parts = explode('/', $path);

		// Discard the first slash.

		array_shift($path_parts);
		$base = array_shift($path_parts);

		// Check if we're on localhost
		if(strpos($base, '~') !== false){

			$base_url[] = '/' . $base;
		};

		$base_url[] = '/';

		return implode('', $base_url);
	}
public static function getBaseURL($url){

		$parsed_url = parse_url($url);
		if($parsed_url === false){

			throw new ErrorException('Malformed URL.');
		}

		$base_url = [];

		if(empty($parsed_url['scheme'])){

			throw new ErrorException('Malformed URL, no scheme.');
		}

		$base_url[] = $parsed_url['scheme'];
		$base_url[] = '://';
		$base_url[] = $parsed_url['host'];

		// Check the path

		$path = $parsed_url['path'];
		$path_parts = explode('/', $path);

		// Discard the first slash.

		array_shift($path_parts);
		$base = array_shift($path_parts);

		// Check if we're on localhost
		if(strpos($base, '~') !== false){

			$base_url[] = '/' . $base;
		};

		$base_url[] = '/';

		return implode('', $base_url);
	}
public static function getBaseURL ( $ url ) { $ parsed_url = parse_url ( $ url ) ; if ( $ parsed_url === false ) { throw new ErrorException ( 'Malformed URL.' ) ; } $ base_url = [ ] ; if ( empty ( $ parsed_url [ 'scheme' ] ) ) { throw new ErrorException ( 'Malformed URL, no scheme.' ) ; } $ base_url [ ] = $ parsed_url [ 'scheme' ] ; $ base_url [ ] = '://' ; $ base_url [ ] = $ parsed_url [ 'host' ] ; // Check the path $ path = $ parsed_url [ 'path' ] ; $ path_parts = explode ( '/' , $ path ) ; // Discard the first slash. array_shift ( $ path_parts ) ; $ base = array_shift ( $ path_parts ) ; // Check if we're on localhost if ( strpos ( $ base , '~' ) !== false ) { $ base_url [ ] = '/' . $ base ; } ; $ base_url [ ] = '/' ; return implode ( '' , $ base_url ) ; }
Get the base URL for a given site URL.

<p>This is the PHP equivalent of the JS Shared.getBaseUrl function.
It can deal with localhost as well.</p>

@param string $url
@return string
@throws ErrorException
Get the base URL for a given site URL .