{"id":166,"date":"2023-02-19T17:28:41","date_gmt":"2023-02-19T09:28:41","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=166"},"modified":"2023-02-19T17:29:22","modified_gmt":"2023-02-19T09:29:22","slug":"php-chinese-gbk-utf8-conversion","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/19\/php-chinese-gbk-utf8-conversion\/","title":{"rendered":"PHP\u4e2d\u6587GBK\u4e0eUTF-8\u7f16\u7801\u4e92\u8f6c"},"content":{"rendered":"<h2>iconv \u548c mb_convert_encoding \u7684\u533a\u522b<\/h2>\n<pre><code>string iconv(string in_charset, string out_charset, string str)<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u6ce8\u610f\uff1a\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u9664\u4e86\u53ef\u4ee5\u6307\u5b9a\u8981\u8f6c\u5316\u5230\u7684\u7f16\u7801\u4ee5\u5916\uff0c\u8fd8\u53ef\u4ee5\u589e\u52a0\u4e24\u4e2a\u540e\u7f00\uff1a\/\/TRANSLIT \u548c \/\/IGNORE\uff0c\u5176\u4e2d \/\/TRANSLIT \u4f1a\u81ea\u52a8\u5c06\u4e0d\u80fd\u76f4\u63a5\u8f6c\u5316\u7684\u5b57\u7b26\u53d8\u6210\u4e00\u4e2a\u6216\u591a\u4e2a\u8fd1\u4f3c\u7684\u5b57\u7b26\uff0c\/\/IGNORE \u4f1a\u5ffd\u7565\u6389\u4e0d\u80fd\u8f6c\u5316\u7684\u5b57\u7b26\uff0c\u800c\u9ed8\u8ba4\u6548\u679c\u662f\u4ece\u7b2c\u4e00\u4e2a\u975e\u6cd5\u5b57\u7b26\u622a\u65ad\u3002<\/p>\n<pre><code>string mb_convert_encoding(string str, string to_encoding [, mixed from_encoding])<\/code><\/pre>\n<p>\u9700\u8981\u5f00\u542f mbstring \u6269\u5c55\u5e93\uff0c\u5728php.ini\u91cc\u8bbe\u7f6e <code>extension=php_mbstring.dll<\/code>\uff0cmb_convert_encoding \u53ef\u4ee5\u6307\u5b9a\u591a\u79cd\u8f93\u5165\u7f16\u7801\uff0c\u5b83\u4f1a\u6839\u636e\u5185\u5bb9\u81ea\u52a8\u8bc6\u522b\uff0c\u4f46\u662f\u6267\u884c\u6548\u7387\u6bd4iconv\u5dee\u592a\u591a\u3002<\/p>\n<p>\u793a\u4f8b\uff1aUTF-8\u4e0eGBK\u4e92\u8f6c<\/p>\n<pre><code class=\"language-php\">$content = &#039;\u4e2d\u534e\u4eba\u6c11\u5171\u548c\u56fd&#039;;\n$gbk = mb_convert_encoding($content, &quot;GBK&quot;, &quot;UTF-8&quot;);\necho $gbk;\necho &quot;&lt;br \/&gt;&quot;;\n$utf8 = iconv(&quot;GBK&quot;, &quot;UTF-8\/\/IGNORE&quot;, $gbk);\necho $utf8;<\/code><\/pre>\n<p>\u793a\u4f8b\uff1aPHP\u5b57\u7b26\u4e32\u7edf\u4e00\u8f6c\u7801\u4e3aGBK\uff0c\u81ea\u52a8\u5224\u65ad\u662f\u5426UTF-8\u5e76\u8f6c\u7801<\/p>\n<pre><code class=\"language-php\">public static function strToGBK($strText)\n{\n    $encode = mb_detect_encoding($strText, array(&#039;UTF-8&#039;,&#039;GB2312&#039;,&#039;GBK&#039;));\n    if($encode == &quot;UTF-8&quot;)\n    {\n        return @iconv(&#039;UTF-8&#039;, &#039;GB18030&#039;, $strText);\n    }\n    else\n    {\n        return $strText;\n    }\n}<\/code><\/pre>\n<p>\u793a\u4f8b\uff1aUTF-8 -&gt; GBK<\/p>\n<pre><code class=\"language-php\">$str = &quot;\u9983\u69dd\u9983\u69dd&quot;;\n\necho iconv(&#039;UTF-8&#039;, &#039;GBK\/\/TRANSLIT&#039;, $str);\necho &quot;&lt;br \/&gt;&quot;;\n\necho iconv(&quot;UTF-8&quot;, &quot;GBK\/\/IGNORE&quot;, $str);\necho &quot;&lt;br \/&gt;&quot;;\n\necho mb_convert_encoding($str, &quot;GBK&quot;, &quot;UTF-8&quot;);\necho &quot;&lt;br \/&gt;&quot;;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>iconv \u548c mb_convert_encoding \u7684\u533a\u522b string iconv(string in_ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69],"tags":[],"class_list":["post-166","post","type-post","status-publish","format-standard","hentry","category-php-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=166"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/166\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}