{"id":21,"date":"2006-01-04T19:21:00","date_gmt":"2006-01-04T19:21:00","guid":{"rendered":"http:\/\/marvelliu.wordpress.com\/2006\/01\/04\/php%e7%9a%84%e7%bc%96%e7%a0%81%e8%bd%ac%e6%8d%a2%e7%9a%84%e9%97%ae%e9%a2%98"},"modified":"2006-01-04T19:21:00","modified_gmt":"2006-01-04T19:21:00","slug":"php%e7%9a%84%e7%bc%96%e7%a0%81%e8%bd%ac%e6%8d%a2%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/marvelliu.space\/?p=21","title":{"rendered":"PHP\u7684\u7f16\u7801\u8f6c\u6362\u7684\u95ee\u9898"},"content":{"rendered":"<p>\u4eca\u5929\u51c6\u5907\u5c06IP Search\u6dfb\u52a0\u5230Blog\u4e2d\uff0c\u7ed3\u679c\u53d1\u73b0\u4e00\u4e2a\u95ee\u9898\uff1a\u7eaf\u771f\u6570\u636e\u5e93\u4f7f\u7528\u4e86gb2312\u7f16\u7801\uff0c\u800cwordpress\u7528\u7684\u662futf-8\u7f16\u7801\u3002\u9875\u9762\u663e\u793a\u7684\u662f\u4e71\u7801\uff0c\u6240\u4ee5\u9700\u8981\u627e\u5230\u4e00\u4e2a\u529e\u6cd5\u5728PHP\u4e2d\u5b9e\u73b0\u4e24\u79cd\u7f16\u7801\u7684\u4e92\u8f6c\u3002<\/p>\n<p>\u770b\u770bPHP  Manual\uff0c\u4f3c\u4e4ePHP\u6ca1\u6709\u50cf.Net\u90a3\u6837\u4e13\u95e8\u63d0\u4f9b\u4e00\u4e2aEncoding\u7684\u7c7b\uff0c\u6240\u4ee5\u53ea\u80fd\u7528\u5176\u4ed6\u65b9\u6cd5\u4e86\u3002\u7f51\u4e0a\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a<\/p>\n<p>\u7b2c\u4e00\u79cd\u662f\u4f7f\u7528iconv \u51fd\u6570\u3002\u4f8b\u5982\uff0c \u4e0b\u9762\u7684\u4f8b\u5b50\u662f\u5229\u7528php\u5c06&#8221;\u5168&#8221;\u8fd9\u4e2d\u7f16\u7801\u8f6c\u6362\u4e3agb2312.\uff1a<\/p>\n<p>[coolcode lang=&#8221;php&#8221;]<br \/>$str = &#8220;TTL\u5168\u5929\u5019\u81ea\u52a8\u805a\u7126&#8221;;<br \/>$str = preg_replace(&#8220;|&amp;#([0-9]{1,5});|&#8221;, &#8220;&#8221;.u2utf82gb(\\1).&#8221;&#8221;, $str);<br \/>$str = &#8220;$str=&#8221;$str&#8221;;&#8221;;<\/p>\n<p>eval($str);<br \/>echo $str;<\/p>\n<p>function u2utf82gb($c){<br \/>$str=&#8221;&#8221;;<br \/>if ($c &lt; 0x80) {<br \/>$str.=$c;<br \/>} else if ($c &lt; 0x800) {<br \/>$str.=chr(0xC0 | $c&gt;&gt;6);<br \/>$str.=chr(0x80 | $c &amp; 0x3F);<br \/>} else if ($c &lt; 0x10000) {<br \/>$str.=chr(0xE0 | $c&gt;&gt;12);<br \/>$str.=chr(0x80 | $c&gt;&gt;6 &amp; 0x3F);<br \/>$str.=chr(0x80 | $c &amp; 0x3F);<br \/>} else if ($c &lt; 0x200000) {<br \/>$str.=chr(0xF0 | $c&gt;&gt;18);<br \/>$str.=chr(0x80 | $c&gt;&gt;12 &amp; 0x3F);<br \/>$str.=chr(0x80 | $c&gt;&gt;6 &amp; 0x3F);<br \/>$str.=chr(0x80 | $c &amp; 0x3F);<br \/>}<br \/>return iconv(&#8216;UTF-8&#8217;, &#8216;GB2312&#8217;, $str);<br \/>}<br \/>?&gt;<br \/>[\/coolcode]<br \/>\u7b2c\u4e8c\u79cd\u662f\u4f7f\u7528JavaScript\u5b9e\u73b0\u7684\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>[coolcode lang=&#8221;javascript&#8221;]<br \/>string utfinfo = &#8220;document.write(&#8220;alert(&#8216;aa\u4f60\u597d\u4e48\uff1f\uff1f&#8217;);&#8221;);&#8221;;<br \/>string gb2312info = string.Empty;<br \/>Encoding utf8 = Encoding.UTF8;<br \/>Encoding gb2312 = Encoding.GetEncoding(&#8220;gb2312&#8221;);<br \/>\/\/ Convert the string into a byte[].<br \/>byte[] unicodeBytes = utf8.GetBytes(utfinfo);<br \/>\/\/ Perform the conversion from one encoding to the other.<br \/>byte[] asciiBytes = Encoding.Convert(utf8, gb2312, unicodeBytes);<br \/>\/\/ Convert the new byte[] into a char[] and then into a string.<br \/>\/\/ This is a slightly different approach to converting to illustrate<br \/>\/\/ the use of GetCharCount\/GetChars.<br \/>char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, asciiBytes.Length)];<br \/>gb2312.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);<br \/>gb2312info = new string(asciiChars);<br \/>[\/coolcode]<\/p>\n<p>\u4f46\u662f\u4e0a\u9762\u4e24\u79cd\u65b9\u6cd5\u7684\u7f3a\u70b9\u5c31\u662f\u592a\u7e41\u4e86\uff0c \u5176\u5b9e\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528autogb\u6765\u5b8c\u6210\u8f6c\u5316\u7684\u5de5\u4f5c\u3002\u7531\u4e8e\u5b83\u662f\u5bf9\u4e8e\u6587\u4ef6\u8fdb\u884c\u64cd\u4f5c\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u4e24\u4e2a\u4e34\u65f6\u6587\u4ef6\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<br \/>[coolcode lang=&#8221;php&#8221;]<br \/>$fd=fopen(&#8220;\/tmp\/.marvel01&#8243;,&#8221;w&#8221;);<br \/>fwrite($fd, $gbstring);<br \/>fclose($fd);<br \/>`autogb -i gb -o utf8 \/tmp\/.marvel02`;<br \/>$fd=fopen(&#8220;\/tmp\/.marvel02&#8243;,&#8221;r&#8221;);<br \/>echo fgets($fd, 1024);<br \/>fclose($fd);<br \/>[\/coolcode]<\/p>\n<p>\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u80fd\u5c06\u4e00\u4e2agb2312\u7684\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3autf-8\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u91cc\u6700\u9700\u8981\u6ce8\u610f\u7684\u662f\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\uff0c\u4e0d\u8981\u7ed9\u522b\u4eba\u4efb\u4f55bug\u53ef\u4ee5\u5229\u7528\u7684\u3002\u5f53\u7136 \uff0c\u4f60\u8fd8\u9700\u8981\u4e0b\u8f7dautogb\u8fd9\u4e2a\u5de5\u5177\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u51c6\u5907\u5c06IP Search\u6dfb\u52a0\u5230Blog\u4e2d\uff0c\u7ed3\u679c\u53d1\u73b0\u4e00\u4e2a\u95ee\u9898\uff1a\u7eaf\u771f\u6570\u636e\u5e93\u4f7f\u7528\u4e86gb2312\u7f16\u7801\uff0c\u800cwordpress\u7528\u7684\u662futf-8\u7f16\u7801\u3002\u9875\u9762\u663e\u793a\u7684\u662f\u4e71\u7801\uff0c\u6240\u4ee5\u9700\u8981\u627e\u5230\u4e00\u4e2a\u529e\u6cd5\u5728PHP\u4e2d\u5b9e\u73b0\u4e24\u79cd\u7f16\u7801\u7684\u4e92\u8f6c\u3002 \u770b\u770bPHP Manual\uff0c\u4f3c\u4e4ePHP\u6ca1\u6709\u50cf.Net\u90a3\u6837\u4e13\u95e8\u63d0\u4f9b\u4e00\u4e2aEncoding\u7684\u7c7b\uff0c\u6240\u4ee5\u53ea\u80fd\u7528\u5176\u4ed6\u65b9\u6cd5\u4e86\u3002\u7f51\u4e0a\u7684\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a \u7b2c\u4e00\u79cd\u662f\u4f7f\u7528iconv \u51fd\u6570\u3002\u4f8b\u5982\uff0c \u4e0b\u9762\u7684\u4f8b\u5b50\u662f\u5229\u7528php\u5c06&#8221;\u5168&#8221;\u8fd9\u4e2d\u7f16\u7801\u8f6c\u6362\u4e3agb2312.\uff1a [coolcode lang=&#8221;php&#8221;]$str = &#8220;TTL\u5168\u5929\u5019\u81ea\u52a8\u805a\u7126&#8221;;$str = preg_replace(&#8220;|&amp;#([0-9]{1,5});|&#8221;, &#8220;&#8221;.u2utf82gb(\\1).&#8221;&#8221;, $str);$str = &#8220;$str=&#8221;$str&#8221;;&#8221;; eval($str);echo $str; function u2utf82gb($c){$str=&#8221;&#8221;;if ($c &lt; 0x80) {$str.=$c;} else if ($c &lt; 0x800) {$str.=chr(0xC0 | $c&gt;&gt;6);$str.=chr(0x80 | $c &amp; 0x3F);} else if ($c &lt; 0x10000) {$str.=chr(0xE0 | $c&gt;&gt;12);$str.=chr(0x80 | $c&gt;&gt;6 &amp; 0x3F);$str.=chr(0x80 | $c &amp; 0x3F);} else if &hellip; <a href=\"https:\/\/marvelliu.space\/?p=21\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;PHP\u7684\u7f16\u7801\u8f6c\u6362\u7684\u95ee\u9898&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-21","post","type-post","status-publish","format-standard","hentry","category-tech"],"_links":{"self":[{"href":"https:\/\/marvelliu.space\/index.php?rest_route=\/wp\/v2\/posts\/21","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/marvelliu.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/marvelliu.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/marvelliu.space\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/marvelliu.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=21"}],"version-history":[{"count":0,"href":"https:\/\/marvelliu.space\/index.php?rest_route=\/wp\/v2\/posts\/21\/revisions"}],"wp:attachment":[{"href":"https:\/\/marvelliu.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/marvelliu.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/marvelliu.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}