函数文档

_wp_translate_php_url_constant_to_key()

💡 云策文档标注

概述

_wp_translate_php_url_constant_to_key() 是一个 WordPress 内部函数,用于将 PHP_URL_* 常量转换为对应的数组键名。它接受一个 PHP_URL_* 常量作为参数,返回相应的字符串键名或 false。

关键要点

  • 函数作用:将 PHP_URL_* 常量(如 PHP_URL_SCHEME)映射到 URL 解析数组的键名(如 'scheme')。
  • 参数:$constant(整数,必需),必须是有效的 PHP_URL_* 常量。
  • 返回值:字符串(对应键名)或 false(如果常量无效)。
  • 内部实现:使用一个关联数组进行常量到键名的转换。
  • 相关函数:与 _get_component_from_parsed_url_array() 配合使用,用于从解析的 URL 数组中检索组件。
  • 版本历史:自 WordPress 4.7.0 引入。

代码示例

function _wp_translate_php_url_constant_to_key( $constant ) {
    $translation = array(
        PHP_URL_SCHEME   => 'scheme',
        PHP_URL_HOST     => 'host',
        PHP_URL_PORT     => 'port',
        PHP_URL_USER     => 'user',
        PHP_URL_PASS     => 'pass',
        PHP_URL_PATH     => 'path',
        PHP_URL_QUERY    => 'query',
        PHP_URL_FRAGMENT => 'fragment',
    );

    if ( isset( $translation[ $constant ] ) ) {
        return $translation[ $constant ];
    } else {
        return false;
    }
}

📄 原文内容

Translates a PHP_URL_* constant to the named array keys PHP uses.

Parameters

$constantintrequired
PHP*URL** constant.

Return

string|false The named key or false.

Source

function _wp_translate_php_url_constant_to_key( $constant ) {
	$translation = array(
		PHP_URL_SCHEME   => 'scheme',
		PHP_URL_HOST     => 'host',
		PHP_URL_PORT     => 'port',
		PHP_URL_USER     => 'user',
		PHP_URL_PASS     => 'pass',
		PHP_URL_PATH     => 'path',
		PHP_URL_QUERY    => 'query',
		PHP_URL_FRAGMENT => 'fragment',
	);

	if ( isset( $translation[ $constant ] ) ) {
		return $translation[ $constant ];
	} else {
		return false;
	}
}

Changelog

Version Description
4.7.0 Introduced.