两个函数在格式化带有英文字符的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);
//<a href="test.html">²âÊÔÒ³Ãæ</a>
echo htmlspecialchars($str);
//<a href="test.html">测试页面</a>
?>
本文探讨了PHP中htmlentities与htmlspecialchars两个函数的区别,特别是在处理包含中文字符的HTML代码时的表现差异。htmlentities会将所有不可显示的字符转换为HTML实体,导致中文字符显示为乱码;而htmlspecialchars仅转换特定的HTML字符。
2340

被折叠的 条评论
为什么被折叠?



