PHP 8.4.0 RC4 available for testing

htmlspecialchars_decode

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

htmlspecialchars_decode 将特殊的 HTML 实体转换回普通字符

说明

htmlspecialchars_decode(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401): string

此函数的作用和 htmlspecialchars() 刚好相反。它将特殊的HTML实体转换回普通字符。

被转换的实体有: &" (没有设置ENT_NOQUOTES 时), ' (设置了 ENT_QUOTES 时), < 以及>

参数

string

要解码的字符串

flags

用下列标记中的一个或多个作为一个位掩码,来指定如何处理引号和使用哪种文档类型。默认为 ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401

有效的 flags 常量
常量名 说明
ENT_COMPAT 转换双引号,不转换单引号。
ENT_QUOTES 单引号和双引号都转换。
ENT_NOQUOTES 单引号和双引号都不转换。
ENT_SUBSTITUTE 使用 Unicode 替换符 U+FFFD (UTF-8) 或 &#xFFFD 替换无效的码区序列(code unit sequence)。而不是返回空字符串。
ENT_HTML401 作为HTML 4.01编码处理。
ENT_XML1 作为XML 1编码处理。
ENT_XHTML 作为XHTML编码处理。
ENT_HTML5 作为HTML 5编码处理。

返回值

返回解码后的字符串。

更新日志

版本 说明
8.1.0 flagsENT_COMPAT 变更为ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401

示例

示例 #1 htmlspecialchars_decode() 示例

<?php
$str
= "<p>this -&gt; &quot;</p>\n";

echo
htmlspecialchars_decode($str);

// 注意,这里的引号不会被转换
echo htmlspecialchars_decode($str, ENT_NOQUOTES);
?>

以上示例会输出:

<p>this -> "</p>
<p>this -> &quot;</p>

参见

添加备注

用户贡献的备注 3 notes

up
2
thomas at xci[ignore_this]teit dot commm
16 years ago
The example for "htmlspecialchars_decode()" below sadly does not work for all PHP4 versions.

Quote from the PHP manual:
"get_html_translation_table() will return the translation table that is used internally for htmlspecialchars() and htmlentities()."

But it does NOT! At least not for PHP version 4.4.2.
This was already reported in a bug report (http://bugs.php.net/bug.php?id=25927), but it was marked as BOGUS.

Proof:
Code:
--------------------
<?php
var_dump
(get_html_translation_table(HTML_SPECIALCHARS,ENT_QUOTES));
var_dump(htmlspecialchars('\'',ENT_QUOTES));
?>
--------------------

Output:
--------------------
array
'"' => '&quot;'
''' => '&#39;'
'<' => '&lt;'
'>' => '&gt;'
'&' => '&amp;'

'&#039;'
--------------------

This comment now is not to report this bug again (though I really believe it is one), but to complete the example and warn people of this pitfall.

To make sure your htmlspecialchars_decode fake for PHP4 works, you should do something like this:

<?php
function htmlspecialchars_decode($string,$style=ENT_COMPAT)
{
$translation = array_flip(get_html_translation_table(HTML_SPECIALCHARS,$style));
if(
$style === ENT_QUOTES){ $translation['&#039;'] = '\''; }
return
strtr($string,$translation);
}
?>

Br, Thomas
up
0
Anonymous
18 years ago
This should be the best way to do it.
(Reposted because the other one seems a bit slower and because those who used the code under called it htmlspecialchars_decode_php4)

<?php

if ( !function_exists('htmlspecialchars_decode') )
{
function
htmlspecialchars_decode($text)
{
return
strtr($text, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
}
}

?>
up
-2
or-k at or-k dot com
19 years ago
that works also with &auml; and &quot; and so on.
get_html_translation_table(HTML_ENTITIES) => offers more characters than HTML_SPECIALCHARS

function htmlspecialchars_decode_PHP4($uSTR)
{
return strtr($uSTR, array_flip(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES)));
}
To Top