wp_kses_hair_parse

The timeline below displays how wordpress function wp_kses_hair_parse has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.5

/**
 * Builds an attribute list from string containing attributes.
 *
 * 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.
 *
 * @since 4.2.3
 *
 * @param string $attr Attribute list from HTML element to closing HTML element tag.
 * @return array|false List of attributes found in $attr. Returns false on failure.
 */
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;
    }
}

WordPress Version: 6.3

/**
 * Builds an attribute list from string containing attributes.
 *
 * 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.
 *
 * @since 4.2.3
 *
 * @param string $attr Attribute list from HTML element to closing HTML element tag.
 * @return array|false List of attributes found in $attr. Returns false on failure.
 */
function wp_kses_hair_parse($attr)
{
    if ('' === $attr) {
        return array();
    }
    // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- don't remove regex indentation
    $regex = '(?:' . '[_a-zA-Z][-_a-zA-Z0-9:.]*' . '|' . '\[\[?[^\[\]]+\]\]?' . ')' . '(?:' . '\s*=\s*' . '(?:' . '"[^"]*"' . '|' . "'[^']*'" . '|' . '[^\s"\']+' . '(?:\s|$)' . ')' . '|' . '(?:\s|$)' . ')' . '\s*';
    // Trailing space is optional except as mentioned above.
    // phpcs:enable
    /*
     * Although it is possible to reduce this procedure to a single regexp,
     * we must run that regexp twice to get exactly the expected result.
     */
    $validation = "%^({$regex})+\$%";
    $extraction = "%{$regex}%";
    if (1 === preg_match($validation, $attr)) {
        preg_match_all($extraction, $attr, $attrarr);
        return $attrarr[0];
    } else {
        return false;
    }
}

WordPress Version: 5.7

/**
 * Builds an attribute list from string containing attributes.
 *
 * 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.
 *
 * @since 4.2.3
 *
 * @param string $attr Attribute list from HTML element to closing HTML element tag.
 * @return array|false List of attributes found in $attr. Returns false on failure.
 */
function wp_kses_hair_parse($attr)
{
    if ('' === $attr) {
        return array();
    }
    // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- don't remove regex indentation
    $regex = '(?:' . '[_a-zA-Z][-_a-zA-Z0-9:.]*' . '|' . '\[\[?[^\[\]]+\]\]?' . ')' . '(?:' . '\s*=\s*' . '(?:' . '"[^"]*"' . '|' . "'[^']*'" . '|' . '[^\s"\']+' . '(?:\s|$)' . ')' . '|' . '(?:\s|$)' . ')' . '\s*';
    // Trailing space is optional except as mentioned above.
    // phpcs:enable
    // Although it is possible to reduce this procedure to a single regexp,
    // we must run that regexp twice to get exactly the expected result.
    $validation = "%^({$regex})+\$%";
    $extraction = "%{$regex}%";
    if (1 === preg_match($validation, $attr)) {
        preg_match_all($extraction, $attr, $attrarr);
        return $attrarr[0];
    } else {
        return false;
    }
}

WordPress Version: 5.5

/**
 * Builds an attribute list from string containing attributes.
 *
 * 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.
 *
 * @since 4.2.3
 *
 * @param string $attr Attribute list from HTML element to closing HTML element tag.
 * @return array|bool List of attributes found in $attr. Returns false on failure.
 */
function wp_kses_hair_parse($attr)
{
    if ('' === $attr) {
        return array();
    }
    // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- don't remove regex indentation
    $regex = '(?:' . '[_a-zA-Z][-_a-zA-Z0-9:.]*' . '|' . '\[\[?[^\[\]]+\]\]?' . ')' . '(?:' . '\s*=\s*' . '(?:' . '"[^"]*"' . '|' . "'[^']*'" . '|' . '[^\s"\']+' . '(?:\s|$)' . ')' . '|' . '(?:\s|$)' . ')' . '\s*';
    // Trailing space is optional except as mentioned above.
    // phpcs:enable
    // Although it is possible to reduce this procedure to a single regexp,
    // we must run that regexp twice to get exactly the expected result.
    $validation = "%^({$regex})+\$%";
    $extraction = "%{$regex}%";
    if (1 === preg_match($validation, $attr)) {
        preg_match_all($extraction, $attr, $attrarr);
        return $attrarr[0];
    } else {
        return false;
    }
}

WordPress Version: 5.1

/**
 * Builds an attribute list from string containing attributes.
 *
 * 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.
 *
 * @since 4.2.3
 *
 * @param string $attr Attribute list from HTML element to closing HTML element tag.
 * @return array|bool List of attributes found in $attr. Returns false on failure.
 */
function wp_kses_hair_parse($attr)
{
    if ('' === $attr) {
        return array();
    }
    // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- don't remove regex indentation
    $regex = '(?:' . '[-a-zA-Z:]+' . '|' . '\[\[?[^\[\]]+\]\]?' . ')' . '(?:' . '\s*=\s*' . '(?:' . '"[^"]*"' . '|' . "'[^']*'" . '|' . '[^\s"\']+' . '(?:\s|$)' . ')' . '|' . '(?:\s|$)' . ')' . '\s*';
    // Trailing space is optional except as mentioned above.
    // phpcs:enable
    // Although it is possible to reduce this procedure to a single regexp,
    // we must run that regexp twice to get exactly the expected result.
    $validation = "%^({$regex})+\$%";
    $extraction = "%{$regex}%";
    if (1 === preg_match($validation, $attr)) {
        preg_match_all($extraction, $attr, $attrarr);
        return $attrarr[0];
    } else {
        return false;
    }
}

WordPress Version: .10

/**
 * Builds an attribute list from string containing attributes.
 *
 * 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.
 *
 * @since 4.2.3
 *
 * @param string $attr Attribute list from HTML element to closing HTML element tag
 * @return array|bool List of attributes found in $attr. Returns false on failure.
 */
function wp_kses_hair_parse($attr)
{
    if ('' === $attr) {
        return array();
    }
    $regex = '(?:' . '[-a-zA-Z:]+' . '|' . '\[\[?[^\[\]]+\]\]?' . ')' . '(?:' . '\s*=\s*' . '(?:' . '"[^"]*"' . '|' . "'[^']*'" . '|' . '[^\s"\']+' . '(?:\s|$)' . ')' . '|' . '(?:\s|$)' . ')' . '\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.
    $validation = "%^({$regex})+\$%";
    $extraction = "%{$regex}%";
    if (1 === preg_match($validation, $attr)) {
        preg_match_all($extraction, $attr, $attrarr);
        return $attrarr[0];
    } else {
        return false;
    }
}