您好,欢迎访问一九零五行业门户网

php把html实体转换为字符的函数html_entity_decode()

实例
把 html 实体转换为字符:
<?php $str = "&lt;&copy; w3cs&ccedil;h&deg;&deg;&brvbar;&sect;&gt;"; echo html_entity_decode($str); ?>
上面代码的 html 输出如下(查看源代码):
<!doctype html> <html> <body> <© w3csçh°°¦§> </body> </html>
上面代码的浏览器输出如下:
<© w3csçh°°¦§>
定义和用法
html_entity_decode() 函数把 html 实体转换为字符。
html_entity_decode() 函数是 htmlentities() 函数的反函数。
语法
html_entity_decode(string,flags,character-set)
参数 描述
string 必需。规定要解码的字符串。
flags 可选。规定如何处理引号以及使用哪种文档类型。可用的引号类型:
ent_compat - 默认。仅解码双引号。
ent_quotes - 解码双引号和单引号。
ent_noquotes - 不解码任何引号。
规定使用的文档类型的附加 flags:
ent_html401 - 默认。作为 html 4.01 处理代码。
ent_html5 - 作为 html 5 处理代码。
ent_xml1 - 作为 xml 1 处理代码。
ent_xhtml - 作为 xhtml 处理代码。
character-set 可选。一个规定了要使用的字符集的字符串。允许的值:
utf-8 - 默认。ascii 兼容多字节的 8 位 unicode
iso-8859-1 - 西欧
iso-8859-15 - 西欧(加入欧元符号 + iso-8859-1 中丢失的法语和芬兰语字母)
cp866 - dos 专用 cyrillic 字符集
cp1251 - windows 专用 cyrillic 字符集
cp1252 - windows 专用西欧字符集
koi8-r - 俄语
big5 - 繁体中文,主要在台湾使用
gb2312 - 简体中文,国家标准字符集
big5-hkscs - 带香港扩展的 big5
shift_jis - 日语
euc-jp - 日语
macroman - mac 操作系统使用的字符集
注释:在 php 5.4 之前的版本,无法被识别的字符集将被忽略并由 iso-8859-1 替代。自 php 5.4 起,无法被识别的字符集将被忽略并由 utf-8 替代。
技术细节
返回值: 返回已转换的字符串。
php 版本: 4.3.0+
更新日志: 在 php 5 中,character-set 参数的默认值改为 utf-8。
在 php 5.4 中,新增了用于规定使用的文档类型的附加 flags:ent_html401、ent_html5、ent_xml1 和 ent_xhtml。
在 php 5.0 中,新增了对多字节编码的支持。
更多实例
实例 1
把一些 html 实体转换为字符:
<?php $str = "jane &amp; &#039;tarzan&#039;"; echo html_entity_decode($str, ent_compat); // will only convert double quotes echo "<br>"; echo html_entity_decode($str, ent_quotes); // converts double and single quotes echo "<br>"; echo html_entity_decode($str, ent_noquotes); // does not convert any quotes ?>
上面代码的 html 输出如下(查看源代码):
<!doctype html> <html> <body> jane & &#039;tarzan&#039;<br> jane & 'tarzan'<br> jane & &#039;tarzan&#039; </body> </html>
上面代码的浏览器输出如下:
jane & 'tarzan' jane & 'tarzan' jane & 'tarzan'
实例 2
通过使用西欧字符集,把一些 html 实体转换为字符:
<?php $str = "my name is &oslash;yvind &aring;sane. i&#039;m norwegian."; echo html_entity_decode($str, ent_quotes, "iso-8859-1"); ?>
the html output of the code above will be (view source):
<!doctype html> <html> <body> my name is øyvind åsane. i'm norwegian. </body> </html>
上面代码的浏览器输出如下:
my name is øyvind åsane. i'm norwegian.
htmlentities(); html_entity_decode(); htmlspecialchars();
$a = '<div><p>11111#11</p></div> /s #1'; $b = htmlentities($a); echo 'htmlentities: '.$b; echo "<br>"; $c = html_entity_decode($b); echo 'html_entity_decode: '.$c; $d = htmlspecialchars($a); echo "htmlspecialchars: ".$d;
htmlentities: <div><p>11111#11</p></div> /s #1 html_entity_decode: 11111#11 /s #1 htmlspecialchars: <div><p>11111#11</p></div> /s #1
以上就是php把html实体转换为字符的函数html_entity_decode()的详细内容。
其它类似信息

推荐信息