{"id":100,"date":"2010-07-28T10:13:45","date_gmt":"2010-07-28T02:13:45","guid":{"rendered":"http:\/\/www.cnmiss.cn\/?p=100"},"modified":"2010-12-02T06:31:06","modified_gmt":"2010-12-02T06:31:06","slug":"htmlspecialchars-%e5%92%8c-htmlentities-%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/www.cnmiss.cn\/?p=100","title":{"rendered":"htmlspecialchars \u548c htmlentities \u7684\u533a\u522b"},"content":{"rendered":"<p>PHP \u624b\u518c\u4e2d\u5bf9 htmlspecialchars \u5199\u9053<br \/>\nThe translations performed are:<\/p>\n<blockquote><p>&#8216;&amp;&#8217; (ampersand) becomes &#8216;&amp;amp;&#8217;<br \/>\n&#8216;&#8221;&#8216; (double quote) becomes &#8216;&amp;quot;&#8217; when ENT_NOQUOTES is not set.<br \/>\n&#8221;&#8217; (single quote) becomes &#8216;&amp;#039;&#8217; only when ENT_QUOTES is set.<br \/>\n&#8216;&lt;&#8216; (less than) becomes &#8216;&amp;lt;&#8217;<br \/>\n&#8216;&gt;&#8217; (greater than) becomes &#8216;&amp;gt;&#8217;<\/p><\/blockquote>\n<p>htmlspecialchars \u53ea\u8f6c\u5316\u4e0a\u9762\u8fd9\u51e0\u4e2ahtml\u4ee3\u7801\uff0c\u800c htmlentities \u5374\u4f1a\u8f6c\u5316\u6240\u6709\u7684html\u4ee3\u7801\uff0c\u8fde\u540c\u91cc\u9762\u7684\u5b83\u65e0\u6cd5\u8bc6\u522b\u7684\u4e2d\u6587\u5b57\u7b26\u4e5f\u7ed9\u8f6c\u5316\u4e86\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u62ff\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u6765\u505a\u6bd4\u8f83\uff1a<\/p>\n<blockquote><p>$str='&lt;a href=&#8221;test.html&#8221;&gt;\u6d4b\u8bd5\u9875\u9762&lt;\/a&gt;&#8217;;<br \/>\necho htmlentities($str);<br \/>\n\/\/ &amp;lt;a href=&amp;quot;test.html&amp;quot;&amp;gt;&amp;sup2;&amp;acirc;&amp;Ecirc;&amp;Ocirc;&amp;Ograve;&amp;sup3;&amp;Atilde;&amp;aelig;&amp;lt;\/a&amp;gt;<br \/>\n\u00a0<br \/>\n$str='&lt;a href=&#8221;test.html&#8221;&gt;\u6d4b\u8bd5\u9875\u9762&lt;\/a&gt;&#8217;;<br \/>\necho htmlspecialchars($str);<br \/>\n\/\/ &amp;lt;a href=&amp;quot;test.html&amp;quot;&amp;gt;\u6d4b\u8bd5\u9875\u9762&amp;lt;\/a&amp;gt;<\/p><\/blockquote>\n<p>\u7ed3\u8bba\u662f\uff0c\u6709\u4e2d\u6587\u7684\u65f6\u5019\uff0c\u6700\u597d\u7528 htmlspecialchars \uff0c\u5426\u5219\u53ef\u80fd\u4e71\u7801<br \/>\n\u53e6\u5916\u53c2\u8003\u4e00\u4e0b\u8fd9\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570<\/p>\n<blockquote><p>function my_excerpt( $html, $len ) {<br \/>\n\u00a0\u00a0\u00a0 \/\/ $html \u5e94\u5305\u542b\u4e00\u4e2a HTML \u6587\u6863\u3002<br \/>\n\u00a0\u00a0\u00a0 \/\/ \u672c\u4f8b\u5c06\u53bb\u6389 HTML \u6807\u8bb0\uff0cjavascript \u4ee3\u7801<br \/>\n\u00a0\u00a0\u00a0 \/\/ \u548c\u7a7a\u767d\u5b57\u7b26\u3002\u8fd8\u4f1a\u5c06\u4e00\u4e9b\u901a\u7528\u7684<br \/>\n\u00a0\u00a0\u00a0 \/\/ HTML \u5b9e\u4f53\u8f6c\u6362\u6210\u76f8\u5e94\u7684\u6587\u672c\u3002<br \/>\n\u00a0\u00a0\u00a0 $search = array (&#8220;&#8216;&lt;script[^&gt;]*?&gt;.*?&lt;\/script&gt;&#8217;si&#8221;,\u00a0 \/\/ \u53bb\u6389 javascript<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;&#8216;&lt;[\\\/\\!]*?[^&lt;&gt;]*?&gt;&#8217;si&#8221;,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ \u53bb\u6389 HTML \u6807\u8bb0<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;&#8216;([\\r\\n])[\\s]+'&#8221;,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ \u53bb\u6389\u7a7a\u767d\u5b57\u7b26<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;&#8216;&amp;(quot|#34);&#8217;i&#8221;,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ \u66ff\u6362 HTML \u5b9e\u4f53<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;&#8216;&amp;(amp|#38);&#8217;i&#8221;,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;&#8216;&amp;(lt|#60);&#8217;i&#8221;,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;&#8216;&amp;(gt|#62);&#8217;i&#8221;,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;&#8216;&amp;(nbsp|#160);&#8217;i&#8221;,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;&#8216;&amp;(iexcl|#161);&#8217;i&#8221;,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;&#8216;&amp;(cent|#162);&#8217;i&#8221;,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;&#8216;&amp;(pound|#163);&#8217;i&#8221;,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;&#8216;&amp;(copy|#169);&#8217;i&#8221;,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;&#8216;&amp;#(\\d+);&#8217;e&#8221;);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ \u4f5c\u4e3a PHP \u4ee3\u7801\u8fd0\u884c<br \/>\n\u00a0\u00a0\u00a0 $replace = array (&#8220;&#8221;,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;&#8221;,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;<a href=\"file:\/\/\\\\1\">\\\\1<\/a>&#8220;,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;\\&#8221;&#8221;,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;&amp;&#8221;,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;&lt;&#8220;,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;&gt;&#8221;,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8221; &#8220;,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 chr(161),<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 chr(162),<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 chr(163),<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 chr(169),<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;chr(<a href=\"file:\/\/\\\\1\">\\\\1<\/a>)&#8221;);<br \/>\n\u00a0\u00a0\u00a0 $text = preg_replace ($search, $replace, $html);<br \/>\n\u00a0\u00a0\u00a0 $text = trim($text);<br \/>\n\u00a0\u00a0\u00a0 return mb_strlen($text) &gt;= $len ? mb_substr($text, 0, $len) : &#8221;;<br \/>\n}<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u624b\u518c\u4e2d\u5bf9 htmlspecialchars \u5199\u9053 The transla &hellip; <a href=\"https:\/\/www.cnmiss.cn\/?p=100\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-100","post","type-post","status-publish","format-standard","hentry","category-xxbj","tag-php"],"_links":{"self":[{"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=\/wp\/v2\/posts\/100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=100"}],"version-history":[{"count":0,"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=\/wp\/v2\/posts\/100\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cnmiss.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}