函数文档

wp_kses_hair_parse()

💡 云策文档标注

概述

wp_kses_hair_parse() 函数用于从包含属性的字符串中构建属性列表,基于 wp_kses_hair() 但不返回多维数组。它不修改输入,但在意外输入时返回 false 而非剥离内容。

关键要点

  • 函数基于 wp_kses_hair(),但输出为单维数组,而非多维数组。
  • 参数 $attr 是必需的,表示从 HTML 元素到闭合标签的属性列表字符串。
  • 返回值为数组或 false:成功时返回属性列表,失败时返回 false。
  • 使用正则表达式解析属性,包括属性名和值(双引号、单引号或非引号格式)。
  • 函数不修改输入字符串,保持原始数据不变。

代码示例

function wp_kses_hair_parse( $attr ) {
    if ( '' === $attr ) {
        return array();
    }

    $regex =
        '(?:
                [_a-zA-Z][-_a-zA-Z0-9:.]* # Attribute name.
            |
                [[?[^[]]+]]?        # Shortcode in the name position implies unfiltered_html.
        )
        (?:                               # Attribute value.
            s*=s*                       # All values begin with "=".
            (?:
                "[^"]*"                   # Double-quoted.
            |
                '[^']*'                # Single-quoted.
            |
                [^s"']+                 # Non-quoted.
                (?:s|$)                  # Must have a space.
            )
        |
            (?:s|$)                      # If attribute has no value, space is required.
        )
        s*                               # Trailing space is optional except as mentioned above.
        ';

    $validation = "/^($regex)+$/x";
    $extraction = "/$regex/x";

    if ( 1 === preg_match( $validation, $attr ) ) {
        preg_match_all( $extraction, $attr, $attrarr );
        return $attrarr[0];
    } else {
        return false;
    }
}

注意事项

  • 函数在 WordPress 4.2.3 版本中引入。
  • 正则表达式使用 x 修饰符,这是必需的,不应移除。
  • 如果属性字符串为空,函数返回空数组。
  • 相关函数 wp_kses_attr_parse() 用于查找 HTML 元素的所有属性。

📄 原文内容

Builds an attribute list from string containing attributes.

Description

Does not modify input. May return “evil” output.
In case of unexpected input, returns false instead of stripping things.

Based on wp_kses_hair() but does not return a multi-dimensional array.

Parameters

$attrstringrequired
Attribute list from HTML element to closing HTML element tag.

Return

array|false List of attributes found in $attr. Returns false on failure.

Source

function wp_kses_hair_parse( $attr ) {
	if ( '' === $attr ) {
		return array();
	}

	$regex =
		'(?:
				[_a-zA-Z][-_a-zA-Z0-9:.]* # Attribute name.
			|
				[[?[^[]]+]]?        # Shortcode in the name position implies unfiltered_html.
		)
		(?:                               # Attribute value.
			s*=s*                       # All values begin with "=".
			(?:
				"[^"]*"                   # Double-quoted.
			|
				'[^']*'                # Single-quoted.
			|
				[^s"']+                 # Non-quoted.
				(?:s|$)                  # Must have a space.
			)
		|
			(?:s|$)                      # If attribute has no value, space is required.
		)
		s*                               # Trailing space is optional except as mentioned above.
		';

	/*
	 * Although it is possible to reduce this procedure to a single regexp,
	 * we must run that regexp twice to get exactly the expected result.
	 *
	 * Note: do NOT remove the `x` modifiers as they are essential for the above regex!
	 */

	$validation = "/^($regex)+$/x";
	$extraction = "/$regex/x";

	if ( 1 === preg_match( $validation, $attr ) ) {
		preg_match_all( $extraction, $attr, $attrarr );
		return $attrarr[0];
	} else {
		return false;
	}
}

Changelog

Version Description
4.2.3 Introduced.