htmlentities2()
云策文档标注
概述
htmlentities2() 是一个 WordPress 函数,用于将文本中的字符转换为 HTML 实体,同时避免对已编码的实体进行重复编码。
关键要点
- 函数接受一个必需参数 $text,表示要转换的文本字符串。
- 返回转换后的字符串,确保已编码的实体(如 &)不被再次转换。
- 内部使用 get_html_translation_table() 获取 HTML 实体转换表,并通过正则表达式处理 & 字符。
- 该函数自 WordPress 1.2.2 版本引入。
代码示例
function htmlentities2( $text ) {
$translation_table = get_html_translation_table( HTML_ENTITIES, ENT_QUOTES );
$translation_table[ chr( 38 ) ] = '&';
return preg_replace( '/&(?![A-Za-z]{0,4}w{2,3};|#[0-9]{2,3};)/', '&', strtr( $text, $translation_table ) );
}
原文内容
Converts entities, while preserving already-encoded entities.
Parameters
$textstringrequired-
The text to be converted.
Source
function htmlentities2( $text ) {
$translation_table = get_html_translation_table( HTML_ENTITIES, ENT_QUOTES );
$translation_table[ chr( 38 ) ] = '&';
return preg_replace( '/&(?![A-Za-z]{0,4}w{2,3};|#[0-9]{2,3};)/', '&', strtr( $text, $translation_table ) );
}
Changelog
| Version | Description |
|---|---|
| 1.2.2 | Introduced. |