_get_component_from_parsed_url_array()
云策文档标注
概述
_get_component_from_parsed_url_array() 函数用于从已解析的 URL 数组中检索特定组件。它基于 PHP 的 parse_url() 函数,提供一致的返回值处理。
关键要点
- 函数接受两个参数:$url_parts(解析后的 URL 数组或 false)和 $component(指定要检索的组件,使用 PHP 预定义常量,默认 -1 返回所有部分)。
- 返回值根据情况不同:解析失败返回 false;成功时返回数组;请求特定组件时,不存在返回 null,存在则返回字符串或整数(如 PHP_URL_PORT)。
- 内部使用 _wp_translate_php_url_constant_to_key() 函数将 PHP_URL_* 常量转换为数组键。
- 该函数由 wp_parse_url() 调用,确保跨 PHP 版本的返回值一致性。
- 自 WordPress 4.7.0 版本引入。
代码示例
function _get_component_from_parsed_url_array( $url_parts, $component = -1 ) {
if ( -1 === $component ) {
return $url_parts;
}
$key = _wp_translate_php_url_constant_to_key( $component );
if ( false !== $key && is_array( $url_parts ) && isset( $url_parts[ $key ] ) ) {
return $url_parts[ $key ];
} else {
return null;
}
}
原文内容
Retrieves a specific component from a parsed URL array.
Parameters
$url_partsarray|falserequired-
The parsed URL. Can be false if the URL failed to parse.
$componentintoptional-
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).Default:
-1
Source
function _get_component_from_parsed_url_array( $url_parts, $component = -1 ) {
if ( -1 === $component ) {
return $url_parts;
}
$key = _wp_translate_php_url_constant_to_key( $component );
if ( false !== $key && is_array( $url_parts ) && isset( $url_parts[ $key ] ) ) {
return $url_parts[ $key ];
} else {
return null;
}
}
Changelog
| Version | Description |
|---|---|
| 4.7.0 | Introduced. |