wp_parse_url

函式
wp_parse_url ( $url, $component = -1 )
引數
  • (string) $url The URL to parse.
    Required:
  • (int) $component The specific component to retrieve. Use one of the PHP predefined constants to specify which one. Defaults to -1 (= return all parts as an array).
    Required:
    Default: -1
返回值
  • (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.
定義位置
相關方法
wp_parse_strwp_parse_argswp_parse_listwp_parse_id_listwp_update_user
引入
4.4.0
棄用
-

wp_parse_url: 這個函式解析一個URL,並返回一個包含其組成部分的關聯陣列,如方案、主機、路徑、查詢和片段。

PHP的parse_url()函式的封裝器,處理不同PHP版本的返回值的一致性。

PHP 5.4.7 擴充套件了 parse_url() 處理非絕對 URL 的許可權,包括無模式和路徑中含有””://””的相對 URL: 這個函式繞過了這些限制,在 PHP 5.2~5.4+ 上提供了一個標準輸出。

其次,在不同的PHP版本中,對查詢中含有””:””的無模式URL的處理是不一致的: 這個函式也是圍繞這些差異而工作的。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_parse_url( $url, $component = -1 ) {
$to_unset = array();
$url = (string) $url;
if ( '//' === substr( $url, 0, 2 ) ) {
$to_unset[] = 'scheme';
$url = 'placeholder:' . $url;
} elseif ( '/' === substr( $url, 0, 1 ) ) {
$to_unset[] = 'scheme';
$to_unset[] = 'host';
$url = 'placeholder://placeholder' . $url;
}
$parts = parse_url( $url );
if ( false === $parts ) {
// Parsing failure.
return $parts;
}
// Remove the placeholder values.
foreach ( $to_unset as $key ) {
unset( $parts[ $key ] );
}
return _get_component_from_parsed_url_array( $parts, $component );
}
function wp_parse_url( $url, $component = -1 ) { $to_unset = array(); $url = (string) $url; if ( '//' === substr( $url, 0, 2 ) ) { $to_unset[] = 'scheme'; $url = 'placeholder:' . $url; } elseif ( '/' === substr( $url, 0, 1 ) ) { $to_unset[] = 'scheme'; $to_unset[] = 'host'; $url = 'placeholder://placeholder' . $url; } $parts = parse_url( $url ); if ( false === $parts ) { // Parsing failure. return $parts; } // Remove the placeholder values. foreach ( $to_unset as $key ) { unset( $parts[ $key ] ); } return _get_component_from_parsed_url_array( $parts, $component ); }
function wp_parse_url( $url, $component = -1 ) {
	$to_unset = array();
	$url      = (string) $url;

	if ( '//' === substr( $url, 0, 2 ) ) {
		$to_unset[] = 'scheme';
		$url        = 'placeholder:' . $url;
	} elseif ( '/' === substr( $url, 0, 1 ) ) {
		$to_unset[] = 'scheme';
		$to_unset[] = 'host';
		$url        = 'placeholder://placeholder' . $url;
	}

	$parts = parse_url( $url );

	if ( false === $parts ) {
		// Parsing failure.
		return $parts;
	}

	// Remove the placeholder values.
	foreach ( $to_unset as $key ) {
		unset( $parts[ $key ] );
	}

	return _get_component_from_parsed_url_array( $parts, $component );
}

常見問題

FAQs
檢視更多 >