htmlentities跟htmlspecialchars的区别

来源:互联网 发布:linux 串口解包 编辑:程序博客网 时间:2024/04/30 14:27
<script type="text/javascript"><!--google_ad_client = "pub-4490194096475053";/* 内容页,300x250,第一屏 */google_ad_slot = "3685991503";google_ad_width = 300;google_ad_height = 250;// --></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

两个函数在格式化带有英文字符的html代码的时候基本没啥问题,但是htmlentities对中文字符也不放过,这样得出来的结果是中文字符部分变为一堆乱码。

The translations performed are:

‘&’ (ampersand) becomes ‘&’

‘”‘ (double quote) becomes ‘”‘ when ENT_NOQUOTES is not set.

”’ (single quote) becomes ”’ only when ENT_QUOTES is set.

‘<’ (less than) becomes ‘<’

‘>’ (greater than) becomes ‘>’


htmlspecialchars只转化上面这几个html代码,而htmlentities却会转化所有的html代码,连同里面的它无法识别的中文字符也给转化了。

<?php

$str='<a href="test.html">测试页面</a>';
echo htmlentities($str);

//&lt;a href=&quot;test.html&quot;&gt;&sup2;&acirc;&Ecirc;&Ocirc;&Ograve;&sup3;&Atilde;&aelig;&lt;/a&gt;

echo htmlspecialchars($str);

//&lt;a href=&quot;test.html&quot;&gt;测试页面&lt;/a&gt;

?>

 

原创粉丝点击