<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[沧海一粟]]></title> 
<link>http://www.dzhope.com/index.php</link> 
<description><![CDATA[Web系统架构与服务器运维,php开发]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[沧海一粟]]></copyright>
<item>
<link>http://www.dzhope.com/post//</link>
<title><![CDATA[PHP中htmlentities跟htmlspecialchars的区别]]></title> 
<author>jed &lt;jed521@163.com&gt;</author>
<category><![CDATA[代码编程]]></category>
<pubDate>Tue, 20 Jan 2009 03:47:09 +0000</pubDate> 
<guid>http://www.dzhope.com/post//</guid> 
<description>
<![CDATA[ 
	很多人都以为htmlentities跟htmlspecialchars的功能是一样的，都是格式化html代码的，我以前也曾这么认为，但是今天我发现并不是这样的。<br/><br/>这两个函数在格式化带有英文字符的html代码的时候基本没啥问题，但是htmlentities对中文字符也不放过，这样得出来的结果是中文字符部分变为一堆乱码。当时做英文站的时候根本就没觉察到这个问题，而今天公司的一个收藏站却因为有有非英文字符而出现了问题，我最终查出来是htmlentities这个函数的问题，同时我也找到了htmlspecialchars这个函数。<br/><br/>对于这两个函数，php手册上都是英文做的解释，其中在htmlentities函数的说明部分有这么一段英文：<br/><br/>This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.<br/><br/>从这句话中我们也可以看出来这两个函数虽然基本功能差不多，但是还是有细微的差别在里面的。再仔细看htmlspecialchars函数里面的一段话：<br/><br/>The translations performed are:<br/><br/>‘&’ (ampersand) becomes ‘&’<br/><br/>‘”‘ (double quote) becomes ‘”‘ when ENT_NOQUOTES is not set.<br/><br/>”’ (single quote) becomes ”’ only when ENT_QUOTES is set.<br/><br/>‘<’ (less than) becomes ‘<’<br/><br/>‘>’ (greater than) becomes ‘>’<br/><br/><br/>可以了解到htmlspecialchars只转化上面这几个html代码，而htmlentities却会转化所有的html代码，连同里面的它无法识别的中文字符也给转化了。<br/><br/>我们可以拿一个简单的例子来做比较：<br/><br/><div class="code"><br/>&lt;?<br/>$str=&#039;&lt;a href=&quot;test.html&quot;&gt;测试页面&lt;/a&gt;&#039;;<br/>echo htmlentities($str);<br/>?&gt;<br/></div><br/><div class="code"><br/>&lt;?<br/>$str=&#039;&lt;a href=&quot;test.html&quot;&gt;测试页面&lt;/a&gt;&#039;;<br/>echo htmlspecialchars($str);<br/>?&gt;<br/></div><br/>有条件的朋友，可以分别运行一下上面的这两段代码，就可以看出两者的差别了。<br/><br/>Tags - <a href="http://www.dzhope.com/tags/htmlentities/" rel="tag">htmlentities</a> , <a href="http://www.dzhope.com/tags/htmlspecialchars/" rel="tag">htmlspecialchars</a>
]]>
</description>
</item><item>
<link>http://www.dzhope.com/post//#blogcomment</link>
<title><![CDATA[[评论] PHP中htmlentities跟htmlspecialchars的区别]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.dzhope.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>