PHP html_entity_decode() 函数
实例
把 HTML 实体转换为字符:
<?php $str = "<© W3Sçh°°¦§>"; echo html_entity_decode($str); ?>
以上代码的 HTML 输出如下(查看源代码):
!DOCTYPE html <html> <body> <? W3S?h????> </body> </html>
以上代码的浏览器输出:
<? W3S?h????>
定义和用法
html_entity_decode() 函数把 HTML 实体转换为字符。
html_entity_decode() 函数是 htmlentities() Inverse function of the function.
Syntax
html_entity_decode(string,flags,character-set)
Parameter | Description |
---|---|
string | Required. Specifies the string to be decoded. |
flags |
Optional. Specifies how quotes are handled and which document type is used. Available quote types:
Additional flags for specifying the document type used:
|
character-set |
Optional. String value, specifies the character set to be used. Allowed values:
Note:In versions before PHP 5.4, unrecognized character sets will be ignored and replaced by ISO-8859-1. Starting from PHP 5.4, unrecognized character sets will be ignored and replaced by UTF-8. |
Technical details
Return value: | Return the converted string |
PHP version: | 4.3.0+ |
Update log:
Version | Description |
---|---|
PHP 5 | character-set The default parameter value is changed to UTF-8. |
PHP 5.4 |
New additional flags for specifying the document type applicable to the translation table were added:
|
PHP 5.3.4 | 新增了对多字节编码的支持。 |
更多实例
例子 1
把 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'
例子 2
通过使用西欧字符集,把 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.