wp_specialchars_decode()
概述
wp_specialchars_decode() 函数用于将特定的 HTML 实体转换回其原始字符,主要处理 &、<、>、" 和 ' 等实体。它支持通过 $quote_style 参数控制引号的解码行为,并兼容旧版 _wp_specialchars() 的值。
关键要点
- 函数核心功能:解码 HTML 实体,包括 &、<、>、" 和 '。
- 参数 $quote_style:可选,控制引号解码方式,如 ENT_COMPAT(仅解码双引号)、ENT_QUOTES(解码单双引号)或 ENT_NOQUOTES(默认,不解码引号)。
- 兼容性:支持旧值 'single' 和 'double',用于向后兼容 _wp_specialchars()。
- 返回值:返回解码后的字符串,若无实体则直接返回原文本。
- 注意事项:函数不处理所有 HTML 实体(如 –),建议使用 html_entity_decode() 处理更广泛实体。
代码示例
function wp_specialchars_decode( $text, $quote_style = ENT_NOQUOTES ) {
$text = (string) $text;
if ( 0 === strlen( $text ) ) {
return '';
}
if ( ! str_contains( $text, '&' ) ) {
return $text;
}
if ( empty( $quote_style ) ) {
$quote_style = ENT_NOQUOTES;
} elseif ( ! in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) ) {
$quote_style = ENT_QUOTES;
}
$single = array(
''' => "'",
''' => "'",
);
$single_preg = array(
'/�*39;/' => "'",
'/�*27;/i' => "'",
);
$double = array(
'"' => '"',
'"' => '"',
'"' => '"',
);
$double_preg = array(
'/�*34;/' => '"',
'/�*22;/i' => '"',
);
$others = array(
'<' => '<',
'>' => '>',
'&' => '&',
'&' => '&',
'&' => '&',
);
$others_preg = array(
'/�*60;/' => '<',
'/�*62;/' => '>',
'/�*38;/' => '&',
'/�*26;/i' => '&',
);
if ( ENT_QUOTES === $quote_style ) {
$translation = array_merge( $single, $double, $others );
$translation_preg = array_merge( $single_preg, $double_preg, $others_preg );
} elseif ( ENT_COMPAT === $quote_style || 'double' === $quote_style ) {
$translation = array_merge( $double, $others );
$translation_preg = array_merge( $double_preg, $others_preg );
} elseif ( 'single' === $quote_style ) {
$translation = array_merge( $single, $others );
$translation_preg = array_merge( $single_preg, $others_preg );
} elseif ( ENT_NOQUOTES === $quote_style ) {
$translation = $others;
$translation_preg = $others_preg;
}
$text = preg_replace( array_keys( $translation_preg ), array_values( $translation_preg ), $text );
return strtr( $text, $translation );
}注意事项
- 此函数不解码所有 HTML 实体,例如 – 不会被处理,需使用 html_entity_decode() 进行更全面的解码。
- 函数内部通过数组映射和正则表达式处理实体,确保高效解码。
Converts a number of HTML entities into their special characters.
Description
Specifically deals with: &, <, >, ", and '.
$quote_style can be set to ENT_COMPAT to decode " entities, or ENT_QUOTES to do both " and '. Default is ENT_NOQUOTES where no quotes are decoded.
Parameters
$textstringrequired-
The text which is to be decoded.
$quote_stylestring|intoptional-
Converts double quotes if set to ENT_COMPAT, both single and double if set to ENT_QUOTES or none if set to ENT_NOQUOTES.
Also compatible with old _wp_specialchars() values; converting single quotes if set to'single', double if set to'double'or both if otherwise set.
Default is ENT_NOQUOTES.More Arguments from _wp_specialchars( … $quote_style )
Converts double quotes if set to ENT_COMPAT, both single and double if set to ENT_QUOTES or none if set to ENT_NOQUOTES.
Converts single and double quotes, as well as converting HTML named entities (that are not also XML named entities) to their code points if set to ENT_XML1. Also compatible with old values; converting single quotes if set to'single', double if set to'double'or both if otherwise set.
Default is ENT_NOQUOTES.Default:
ENT_NOQUOTES
Source
function wp_specialchars_decode( $text, $quote_style = ENT_NOQUOTES ) {
$text = (string) $text;
if ( 0 === strlen( $text ) ) {
return '';
}
// Don't bother if there are no entities - saves a lot of processing.
if ( ! str_contains( $text, '&' ) ) {
return $text;
}
// Match the previous behavior of _wp_specialchars() when the $quote_style is not an accepted value.
if ( empty( $quote_style ) ) {
$quote_style = ENT_NOQUOTES;
} elseif ( ! in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) ) {
$quote_style = ENT_QUOTES;
}
// More complete than get_html_translation_table( HTML_SPECIALCHARS ).
$single = array(
''' => ''',
''' => ''',
);
$single_preg = array(
'/�*39;/' => ''',
'/�*27;/i' => ''',
);
$double = array(
'"' => '"',
'"' => '"',
'"' => '"',
);
$double_preg = array(
'/�*34;/' => '"',
'/�*22;/i' => '"',
);
$others = array(
'<' => '<',
'<' => '<',
'>' => '>',
'>' => '>',
'&' => '&',
'&' => '&',
'&' => '&',
);
$others_preg = array(
'/�*60;/' => '<',
'/�*62;/' => '>',
'/�*38;/' => '&',
'/�*26;/i' => '&',
);
if ( ENT_QUOTES === $quote_style ) {
$translation = array_merge( $single, $double, $others );
$translation_preg = array_merge( $single_preg, $double_preg, $others_preg );
} elseif ( ENT_COMPAT === $quote_style || 'double' === $quote_style ) {
$translation = array_merge( $double, $others );
$translation_preg = array_merge( $double_preg, $others_preg );
} elseif ( 'single' === $quote_style ) {
$translation = array_merge( $single, $others );
$translation_preg = array_merge( $single_preg, $others_preg );
} elseif ( ENT_NOQUOTES === $quote_style ) {
$translation = $others;
$translation_preg = $others_preg;
}
// Remove zero padding on numeric entities.
$text = preg_replace( array_keys( $translation_preg ), array_values( $translation_preg ), $text );
// Replace characters according to translation table.
return strtr( $text, $translation );
}
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |
Skip to note 2 content
Noptin Newsletter Team
Beware that this function does not decode all entities. For example, it does not decode
–.If you encounter such an issue, try using
html_entity_decode().