在PHP中,字符串函数 html_entity_decode() 用于把HTML实体转换为字符。
函数语法:
html_entity_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") ]] ) : string
函数参数说明:
参数 | 描述 |
---|---|
string | 必需。规定要解码的字符串。 |
flags | 可选。规定如何处理引号以及使用哪种文档类型。 可用的引号类型:
规定使用的文档类型的附加 flags:
|
encoding | 可选。一个规定了要使用的字符集的字符串。 允许的值:
注释:在 PHP 5.4 之前的版本,无法被识别的字符集将被忽略并由 ISO-8859-1 替代。自 PHP 5.4 起,无法被识别的字符集将被忽略并由 UTF-8 替代 |
html_entity_decode() 函数是 htmlentities() 函数的反函数。用于把HTML实体转换为字符。
举例1,只解码双引号:
<?php $str = "hello "world",& 'china'"; // 只解码双引号 echo html_entity_decode($str, ENT_COMPAT);
以上代码输出的HTML代码如下(只解码了双引号):
hello "world",& 'china'
在浏览器中输出如下:
hello "world",& 'china'
举例2,解码双引号和单引号:
<?php $str = "hello "world",& 'china'"; // 解码双引号和单引号 echo html_entity_decode($str, ENT_QUOTES);
以上代码输出的HTML代码如下(解码了双引号和单引号):
hello "world",& 'china'
在浏览器中输出如下:
hello "world",& 'china'
举例3,不解码任何引号:
<?php $str = "hello "world",& 'china'"; // 不解码任何引号 echo html_entity_decode($str, ENT_NOQUOTES);
以上代码输出的HTML代码如下(解码了双引号和单引号):
hello "world",& 'china'
在浏览器中输出如下:
hello "world",& 'china'