{"id":762,"date":"2013-10-20T15:47:13","date_gmt":"2013-10-20T07:47:13","guid":{"rendered":"http:\/\/blog.inout.asia\/?p=762"},"modified":"2015-02-27T03:39:46","modified_gmt":"2015-02-26T19:39:46","slug":"%e5%be%b7%e9%97%ae%ef%bc%9autf-8%e4%b8%8b%e7%9a%84php%e5%85%a8%e8%a7%92%e6%a0%87%e7%82%b9%e8%bd%ac%e4%b8%ba%e5%8d%8a%e8%a7%92%e7%9a%84%e7%96%91%e9%97%ae","status":"publish","type":"post","link":"https:\/\/blog.my0513.com\/?p=762","title":{"rendered":"\u5fb7\u95ee\uff1aUTF-8\u4e0b\u7684PHP\u5168\u89d2\u6807\u70b9\u8f6c\u4e3a\u534a\u89d2\u7684\u7591\u95ee"},"content":{"rendered":"<p>\u4e00\u3001PHP\u5168\u89d2\u6807\u70b9\u8f6c\u4e3a\u534a\u89d2<\/p>\n<p><code>&lt; ?php<br \/>\n$str = \"\uff10\uff11\uff12\uff13\uff21\uff22\uff23\uff24\uff26\uff37\uff33\uff3c\uff02\uff0c\uff0e\uff1f\uff1c\uff1e\uff5b\uff5d\uff3b\uff3d\uff0a\uff06\uff3e\uff05\uff03\uff20\uff01\uff5e\uff08\uff09\uff0b\uff0d\uff5c\uff1a\uff1b\";<br \/>\necho \"$str\";<br \/>\necho \"<br \/>\n\";<br \/>\n$str = preg_replace('\/\\xa3([\\xa1-\\xfe])\/e', 'chr(ord(\\1)-0x80)', $str);<br \/>\necho $str;<br \/>\n<\/code><br \/>\n\u8fd9\u662f\u7f51\u4e0a\u770b\u6765\u7684\u4ee3\u7801\uff0c\u6240\u6709\u7684\u4e2d\u6587\u6807\u70b9\u7684\u7b2c\u4e8c\u4e2a\u5b57\u8282\u51cf\u53bb0X80\uff08\u5373128\uff09\u6240\u5f97\u7684\u6570\u5b57\u5c31\u662f\u534a\u89d2\u6240\u5f97\u7684\u6570\u5b57\u4e86\u3002\u800c\/e\u6a21\u5f0f\u8868\u8fbe\u7684\u662f\uff1a\u5982\u679c\u8bbe\u5b9a\u4e86\u6b64\u4fee\u6b63\u7b26\uff0cpreg_replace() \u5728\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u5bf9\u9006\u5411\u5f15\u7528\u4f5c\u6b63\u5e38\u7684\u66ff\u6362\uff0c\u5c06\u5176\u4f5c\u4e3a PHP \u4ee3\u7801\u6c42\u503c\uff0c\u5e76\u7528\u5176\u7ed3\u679c\u6765\u66ff\u6362\u6240\u641c\u7d22\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u5728\u975eUTF-8\u6a21\u5f0f\u4e0b\u8fd9\u4e2a\u51fd\u6570\u662f\u53ef\u884c\u7684,\u4f46\u662fUTF-8\u4e0b \u8fd9\u4e2a\u65b9\u6cd5\u5c31\u4f3c\u4e4e\u65e0\u6548,\u6c42\u80fd\u6ee1\u8db3UTF-8\u6a21\u5f0f\u4e0b\u7684\u8fd9\u4e2a\u529f\u80fd\u7684\u5b9e\u73b0&#8230;<\/p>\n<p>\u4e8c\u3001\u5982\u679c\u6587\u4ef6\u662futf8\u7684\uff0c\u90a3\u4e48\u53ef\u4ee5\u901a\u8fc7<br \/>\n$str = iconv(&#8216;utf-8&#8217;, &#8216;gbk&#8217;, $str); \u8f6c\u6362\u7f16\u7801\u540e\uff0c\u518d<br \/>\npreg_replace(&#8216;\/\\xa3([\\xa1-\\xfe])\/e&#8217;, &#8216;chr(ord(\\1)-0x80)&#8217;, $str);<\/p>\n<p>\u6709\u4e9b\u5b57\u7b26\u5728utf-8\u4e2d\u53ef\u4ee5\u8868\u793a\uff0c\u800c\u5728gbk\u4e2d\u65e0\u6cd5\u8868\u793a\uff0c\u90a3\u4e48\u4f60\u5c06utf-8\u8f6c\u6210gbk\u4e0d\u4f1a\u5bfc\u81f4\u5b57\u7b26\u4e22\u5931\u5417\uff1f\u53ef\u4ee5\u4f7f\u7528IGNORE\uff1a<br \/>\n$str = iconv(&#8216;utf-8&#8217;, &#8216;gbk\/\/IGNORE&#8217;, $str);<\/p>\n<p>\u4e0b\u8fb9\u662f\u4e0d\u540c\u7f16\u7801\u5bf9\u5e94\u7684\u6b63\u5219\u5339\u914d\u7f16\u7801\u7684\u8303\u56f4\uff1a<br \/>\n<code>UTF8: [\\x01-\\x7f]|[\\xc0-\\xdf][\\x80-\\xbf]|[\\xe0-\\xef][\\x80-\\xbf]{2}|[\\xf0-\\xff][\\x80-\\xbf]{3}<br \/>\nUTF16: [\\x00-\\xd7][\\xe0-\\xff]|[\\xd8-\\xdf][\\x00-\\xff]{2}<br \/>\nBig5: [\\x01-\\x7f]|[\\x81-\\xfe]([\\x40-\\x7e]|[\\xa1-\\xfe])<br \/>\nGBK :[\\x01-\\x7f]|[\\x81-\\xfe][\\x40-\\xfe]<br \/>\nGB2312\u6c49\u5b57: [\\xb0-\\xf7][\\xa0-\\xfe]<br \/>\nGB2312\u534a\u89d2\u6807\u70b9\u7b26\u53f7\u53ca\u7279\u6b8a\u7b26\u53f7: \\xa1[\\xa2-\\xfe]<br \/>\nGB2312\u7f57\u9a6c\u6570\u7ec4\u53ca\u9879\u76ee\u5e8f\u53f7: \\xa2([\\xa1-\\xaa]|[\\xb1-\\xbf]|[\\xc0-\\xdf]|[\\xe0-\\xe2]|[\\xe5-\\xee]|[\\xf1-\\xfc])<br \/>\nGB2312\u5168\u89d2\u6807\u70b9\u53ca\u5168\u89d2\u5b57\u6bcd: \\xa3[\\xa1-\\xfe]<br \/>\nGB2312\u65e5\u6587\u5e73\u5047\u540d: \\xa4[\\xa1-\\xf3]<br \/>\nGB2312\u65e5\u6587\u7247\u5047\u540d:\\xa5[\\xa1-\\xf6]<br \/>\nGB18030: [\\x00-\\x7f]|[\\x81-\\xfe][\\x40-\\xfe]|[\\x81-\\xfe][\\x30-\\x39][\\x81-\\xfe][\\x30-\\x39] <\/code><\/p>\n<p>JIS [\\x20-\\x7e]|[\\x21-\\x5f]|[\\x21-\\x7e]{2}<br \/>\nSJIS [\\x20-\\x7e]|[\\xa1-\\xdf]|([\\x81-\\x9f]|[\\xe0-\\xef])([\\x40-\\x7e]|[\\x80-\\xfc])<br \/>\nSJIS\u5168\u89d2\u7a7a\u683c (?:\\x81\\x81)<br \/>\nSJIS\u5168\u89d2\u6570\u5b57 (?:\\x82[\\x4f-\\x58])<br \/>\nSJIS\u5168\u89d2\u5927\u5199\u82f1\u6587 (?:\\x82[\\x60-\\x79])<br \/>\nSJIS\u5168\u89d2\u5c0f\u5199\u82f1\u6587 (?:\\x82[\\x81-\\x9a])<br \/>\nSJIS\u5168\u89d2\u5e73\u5047\u540d (?:\\x82[\\x9f-\\xf1])<br \/>\nSJIS\u5168\u89d2\u5e73\u5047\u540d\u6269\u5c55 (?:\\x82[\\x9f-\\xf1]|\\x81[\\x4a\\x4b\\x54\\x55])<br \/>\nSJIS\u5168\u89d2\u7247\u5047\u540d (?:\\x83[\\x40-\\x96])<br \/>\nSJIS\u5168\u89d2\u7247\u5047\u540d\u6269\u5c55 (?:\\x83[\\x40-\\x96]|\\x81[\\x45\\x5b\\x52\\x53])<\/p>\n<p>EUC_JP [\\x20-\\x7e]|\\x81[\\xa1-\\xdf]|[\\xa1-\\xfe][\\xa1-\\xfe]|\\x8f[\\xa1-\\xfe]{2}<br \/>\nEUC_JP\u6807\u70b9\u7b26\u53f7\u53ca\u7279\u6b8a\u5b57\u7b26 [\\xa1-\\xa2][\\xa0-\\xfe]<br \/>\nEUC_JP\u5168\u89d2\u6570\u5b57 \\xa3[\\xb0-\\xb9]<br \/>\nEUC_JP\u5168\u89d2\u5927\u5199\u82f1\u6587 \\xa3[\\xc1-\\xda]<br \/>\nEUC_JP\u5168\u89d2\u5c0f\u5199\u82f1\u6587 \\xa3[\\xe1-\\xfa]<br \/>\nEUC_JP\u5168\u89d2\u5e73\u5047\u540d \\xa4[\\xa1-\\xf3]<br \/>\nEUC_JP\u5168\u89d2\u7247\u5047\u540d \\xa3[\\xb0-\\xb9]|\\xa3[\\xc1-\\xda]|\\xa5[\\xa1-\\xf6][\\xa3][\\xb0-\\xfa]|[\\xa1][\\xbc-\\xbe]|[\\xa1][\\xdd]<br \/>\nEUC_JP\u5168\u89d2\u6c49\u5b57 [\\xb0-\\xcf][\\xa0-\\xd3]|[\\xd0-\\xf4][\\xa0-\\xfe]|[\\xB0-\\xF3][\\xA1-\\xFE]|[\\xF4][\\xA1-\\xA6]|[\\xA4][\\xA1-\\xF3]|[\\xA5][\\xA1-\\xF6]|[\\xA1][\\xBC-\\xBE]<br \/>\nEUC_JP\u5168\u89d2\u7a7a\u683c (?:\\xa1\\xa1)<\/p>\n<p>EUC\u534a\u89d2\u7247\u5047\u540d (?:\\x8e[\\xa6-\\xdf])<br \/>\n\u65e5\u6587\u534a\u89d2\u7a7a\u683c \\x20<\/p>\n<p>\u4e09\u3001\u6211\u89c9\u5f97\u6ca1\u5fc5\u8981\u90a3\u4e48\u9ebb\u70e6\uff0c\u76f4\u63a5\u5199\u4e2a\u65b9\u6cd5\u66ff\u6362\u5c31\u5b8c\u4e86\uff0c\u800c\u4e14\u6b63\u5219\u7684\u6548\u7387\u8fd8\u4f4e\uff0c\u6211\u5c31\u662f\u8fd9\u4e48\u5f04\u7684\uff1a<br \/>\n<code>\/**<br \/>\n* \u5b57\u7b26\u4e32\u534a\u89d2\u548c\u5168\u89d2\u95f4\u76f8\u4e92\u8f6c\u6362<br \/>\n* @param string $str \u5f85\u8f6c\u6362\u7684\u5b57\u7b26\u4e32<br \/>\n* @param int $type TODBC:\u8f6c\u6362\u4e3a\u534a\u89d2\uff1bTOSBC\uff0c\u8f6c\u6362\u4e3a\u5168\u89d2<br \/>\n* @return string \u8fd4\u56de\u8f6c\u6362\u540e\u7684\u5b57\u7b26\u4e32<br \/>\n*\/<br \/>\nfunction convertStrType($str, $type) {<\/code><\/p>\n<p>$dbc = array(<br \/>\n&#8216;\uff10&#8217; , &#8216;\uff11&#8217; , &#8216;\uff12&#8217; , &#8216;\uff13&#8217; , &#8216;\uff14&#8217; ,<br \/>\n&#8216;\uff15&#8217; , &#8216;\uff16&#8217; , &#8216;\uff17&#8217; , &#8216;\uff18&#8217; , &#8216;\uff19&#8217; ,<br \/>\n&#8216;\uff21&#8217; , &#8216;\uff22&#8217; , &#8216;\uff23&#8217; , &#8216;\uff24&#8217; , &#8216;\uff25&#8217; ,<br \/>\n&#8216;\uff26&#8217; , &#8216;\uff27&#8217; , &#8216;\uff28&#8217; , &#8216;\uff29&#8217; , &#8216;\uff2a&#8217; ,<br \/>\n&#8216;\uff2b&#8217; , &#8216;\uff2c&#8217; , &#8216;\uff2d&#8217; , &#8216;\uff2e&#8217; , &#8216;\uff2f&#8217; ,<br \/>\n&#8216;\uff30&#8217; , &#8216;\uff31&#8217; , &#8216;\uff32&#8217; , &#8216;\uff33&#8217; , &#8216;\uff34&#8217; ,<br \/>\n&#8216;\uff35&#8217; , &#8216;\uff36&#8217; , &#8216;\uff37&#8217; , &#8216;\uff38&#8217; , &#8216;\uff39&#8217; ,<br \/>\n&#8216;\uff3a&#8217; , &#8216;\uff41&#8217; , &#8216;\uff42&#8217; , &#8216;\uff43&#8217; , &#8216;\uff44&#8217; ,<br \/>\n&#8216;\uff45&#8217; , &#8216;\uff46&#8217; , &#8216;\uff47&#8217; , &#8216;\uff48&#8217; , &#8216;\uff49&#8217; ,<br \/>\n&#8216;\uff4a&#8217; , &#8216;\uff4b&#8217; , &#8216;\uff4c&#8217; , &#8216;\uff4d&#8217; , &#8216;\uff4e&#8217; ,<br \/>\n&#8216;\uff4f&#8217; , &#8216;\uff50&#8217; , &#8216;\uff51&#8217; , &#8216;\uff52&#8217; , &#8216;\uff53&#8217; ,<br \/>\n&#8216;\uff54&#8217; , &#8216;\uff55&#8217; , &#8216;\uff56&#8217; , &#8216;\uff57&#8217; , &#8216;\uff58&#8217; ,<br \/>\n&#8216;\uff59&#8217; , &#8216;\uff5a&#8217; , &#8216;\uff0d&#8217; , &#8216;\u3000&#8217; , &#8216;\uff1a&#8217; ,<br \/>\n&#8216;\uff0e&#8217; , &#8216;\uff0c&#8217; , &#8216;\uff0f&#8217; , &#8216;\uff05&#8217; , &#8216;\uff03&#8217; ,<br \/>\n&#8216;\uff01&#8217; , &#8216;\uff20&#8217; , &#8216;\uff06&#8217; , &#8216;\uff08&#8217; , &#8216;\uff09&#8217; ,<br \/>\n&#8216;\uff1c&#8217; , &#8216;\uff1e&#8217; , &#8216;\uff02&#8217; , &#8216;\uff07&#8217; , &#8216;\uff1f&#8217; ,<br \/>\n&#8216;\uff3b&#8217; , &#8216;\uff3d&#8217; , &#8216;\uff5b&#8217; , &#8216;\uff5d&#8217; , &#8216;\uff3c&#8217; ,<br \/>\n&#8216;\uff5c&#8217; , &#8216;\uff0b&#8217; , &#8216;\uff1d&#8217; , &#8216;\uff3f&#8217; , &#8216;\uff3e&#8217; ,<br \/>\n&#8216;\uffe5&#8217; , &#8216;\uffe3&#8217; , &#8216;\uff40&#8217;<\/p>\n<p>);<\/p>\n<p>$sbc = array( \/\/\u534a\u89d2<br \/>\n&#8216;0&#8217;, &#8216;1&#8217;, &#8216;2&#8217;, &#8216;3&#8217;, &#8216;4&#8217;,<br \/>\n&#8216;5&#8217;, &#8216;6&#8217;, &#8216;7&#8217;, &#8216;8&#8217;, &#8216;9&#8217;,<br \/>\n&#8216;A&#8217;, &#8216;B&#8217;, &#8216;C&#8217;, &#8216;D&#8217;, &#8216;E&#8217;,<br \/>\n&#8216;F&#8217;, &#8216;G&#8217;, &#8216;H&#8217;, &#8216;I&#8217;, &#8216;J&#8217;,<br \/>\n&#8216;K&#8217;, &#8216;L&#8217;, &#8216;M&#8217;, &#8216;N&#8217;, &#8216;O&#8217;,<br \/>\n&#8216;P&#8217;, &#8216;Q&#8217;, &#8216;R&#8217;, &#8216;S&#8217;, &#8216;T&#8217;,<br \/>\n&#8216;U&#8217;, &#8216;V&#8217;, &#8216;W&#8217;, &#8216;X&#8217;, &#8216;Y&#8217;,<br \/>\n&#8216;Z&#8217;, &#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;, &#8216;d&#8217;,<br \/>\n&#8216;e&#8217;, &#8216;f&#8217;, &#8216;g&#8217;, &#8216;h&#8217;, &#8216;i&#8217;,<br \/>\n&#8216;j&#8217;, &#8216;k&#8217;, &#8216;l&#8217;, &#8216;m&#8217;, &#8216;n&#8217;,<br \/>\n&#8216;o&#8217;, &#8216;p&#8217;, &#8216;q&#8217;, &#8216;r&#8217;, &#8216;s&#8217;,<br \/>\n&#8216;t&#8217;, &#8216;u&#8217;, &#8216;v&#8217;, &#8216;w&#8217;, &#8216;x&#8217;,<br \/>\n&#8216;y&#8217;, &#8216;z&#8217;, &#8216;-&#8216;, &#8216; &#8216;, &#8216;:&#8217;,<br \/>\n&#8216;.&#8217;, &#8216;,&#8217;, &#8216;\/&#8217;, &#8216;%&#8217;, &#8216; #&#8217;,<br \/>\n&#8216;!&#8217;, &#8216;@&#8217;, &#8216;&amp;&#8217;, &#8216;(&#8216;, &#8216;)&#8217;,<br \/>\n&#8216;&lt; &#8216;, &#8216;&gt;&#8217;, &#8216;&#8221;&#8216;, &#8216;\\&#8221;,&#8217;?&#8217;,<br \/>\n&#8216;[&#8216;, &#8216;]&#8217;, &#8216;{&#8216;, &#8216;}&#8217;, &#8216;\\\\&#8217;,<br \/>\n&#8216;|&#8217;, &#8216;+&#8217;, &#8216;=&#8217;, &#8216;_&#8217;, &#8216;^&#8217;,<br \/>\n&#8216;\uffe5&#8217;,&#8217;~&#8217;, &#8216;`&#8217;<\/p>\n<p>);<br \/>\nif($type == &#8216;TODBC&#8217;){<br \/>\nreturn str_replace( $sbc, $dbc, $str ); \/\/\u534a\u89d2\u5230\u5168\u89d2<br \/>\n}elseif($type == &#8216;TOSBC&#8217;){<br \/>\nreturn str_replace( $dbc, $sbc, $str ); \/\/\u5168\u89d2\u5230\u534a\u89d2<br \/>\n}else{<br \/>\nreturn $str;<br \/>\n}<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001PHP\u5168\u89d2\u6807\u70b9\u8f6c\u4e3a\u534a\u89d2 &lt; ?php $str = &#8220;\uff10\uff11\uff12\uff13\uff21\uff22\uff23\uff24\uff26\uff37\uff33\uff3c\uff02\uff0c\uff0e\uff1f\uff1c\uff1e\uff5b\uff5d\uff3b\uff3d\uff0a [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43,7,1,45],"tags":[85,102,103],"class_list":["post-762","post","type-post","status-publish","format-standard","hentry","category-open","category-7","category-1","category-45","tag-php","tag-102","tag-103"],"_links":{"self":[{"href":"https:\/\/blog.my0513.com\/index.php?rest_route=\/wp\/v2\/posts\/762","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.my0513.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.my0513.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.my0513.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.my0513.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=762"}],"version-history":[{"count":4,"href":"https:\/\/blog.my0513.com\/index.php?rest_route=\/wp\/v2\/posts\/762\/revisions"}],"predecessor-version":[{"id":807,"href":"https:\/\/blog.my0513.com\/index.php?rest_route=\/wp\/v2\/posts\/762\/revisions\/807"}],"wp:attachment":[{"href":"https:\/\/blog.my0513.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.my0513.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.my0513.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}