WP_CLI\Utils

parse_url()WP-CLI 1.0

Helper function to use wp_parse_url when available or fall back to PHP's parse_url if not.

Additionally, this adds 'http://' to the URL if no scheme was found.

No Hooks.

Return

Mixed. False on parse failure; Array of URL components on success; When a specific component has been requested: null if the component doesn't exist in the given URL; a string or - in the case of PHP_URL_PORT - integer when it does. See parse_url()'s return values.

Usage

parse_url( $url, $component, $auto_add_scheme );
$url(string) (required)
The URL to parse.
$component(int)
The specific component to retrieve. Use one of the PHP predefined constants to specify which one.
Default: -1 (= return all parts as an array)
$auto_add_scheme(true|false)
Automatically add an http:// scheme if none was found.
Default: true

parse_url() code WP-CLI 2.8.0-alpha

function parse_url( $url, $component = - 1, $auto_add_scheme = true ) {
	if (
		function_exists( 'wp_parse_url' )
		&& (
			-1 === $component
			|| wp_version_compare( '4.7', '>=' )
		)
	) {
		$url_parts = wp_parse_url( $url, $component );
	} else {
		// phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url -- Fallback.
		$url_parts = \parse_url( $url, $component );
	}

	// phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url -- Own version based on WP one.
	if ( $auto_add_scheme && ! parse_url( $url, PHP_URL_SCHEME, false ) ) {
		// phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url -- Own version based on WP one.
		$url_parts = parse_url( 'http://' . $url, $component, false );
	}

	return $url_parts;
}