html_entity_decode
将HTML实体转换为相应的字符
html_entity_decode()
函数把 HTML 实体转换为字符。
html_entity_decode()
函数是 htmlentities()
函数的反函数。
把 HTML 实体转换为字符:
<?php $str = "<? W3S?h°°|§>"; echo html_entity_decode($str); ?>
以上代码的 HTML 输出如下(查看源代码):
<!DOCTYPE html> <html> <body> <? W3S?h????> </body> </html>
以上代码的浏览器输出:
<? W3S?h????>
把 HTML 实体转换为字符:
<?php $str = "Bill & 'Steve'"; echo html_entity_decode($str, ENT_COMPAT); // 只转换双引号 echo "<br>"; echo html_entity_decode($str, ENT_QUOTES); // 转换双引号和单引号 echo "<br>"; echo html_entity_decode($str, ENT_NOQUOTES); // 不转换任何引号 ?>
以上代码的 HTML 输出(查看源代码):
<!DOCTYPE html> <html> <body> Bill & 'Steve'<br> Bill & 'Steve'<br> Bill & 'Steve' </body> </html>
以上代码的浏览器输出:
Bill & 'Steve' Bill & 'Steve' Bill & 'Steve'
通过使用西欧字符集,把 HTML 实体转换为字符:
<?php $str = "My name is ?yvind ?sane. I'm Norwegian."; echo html_entity_decode($str, ENT_QUOTES, "ISO-8859-1"); ?>
以上代码的 HTML 输出(查看源代码):
<!DOCTYPE html> <html> <body> My name is ?yvind ?sane. I'm Norwegian. </body> </html>
以上代码的浏览器输出:
My name is ?yvind ?sane. I'm Norwegian.
html_entity_decode(string,flags,character-set)
参数 | 描述 |
---|---|
string | 必需。规定要解码的字符串。 |
flags |
可选。规定如何处理引号以及使用哪种文档类型。 可用的引号类型:
规定所使用文档类型的附加 flags:
|
character-set |
可选。字符串值,规定要使用的字符集。 允许的值:
注释:在 PHP 5.4 之前的版本,无法被识别的字符集将被忽略并由 ISO-8859-1 替代。自 PHP 5.4 起,无法被识别的字符集将被忽略并由 UTF-8 替代。 |