{"id":679,"date":"2025-12-27T07:59:24","date_gmt":"2025-12-26T23:59:24","guid":{"rendered":"https:\/\/www.idcyz.com\/blog\/?p=679"},"modified":"2025-12-27T07:59:24","modified_gmt":"2025-12-26T23:59:24","slug":"php%e5%ae%9e%e7%8e%b0gbk%e8%bd%acutf-8%e7%9a%84%e5%a4%9a%e7%a7%8d%e6%96%b9%e6%b3%95%e4%b8%8e%e5%ba%94%e7%94%a8%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.idcyz.com\/blog\/679.html","title":{"rendered":"PHP\u5b9e\u73b0GBK\u8f6cUTF-8\u7684\u591a\u79cd\u65b9\u6cd5\u4e0e\u5e94\u7528\u5206\u6790"},"content":{"rendered":"<p><p>\u5728\u73b0\u4ee3\u7684\u7f51\u9875\u5f00\u53d1\u4e2d\uff0c\u5b57\u7b26\u7f16\u7801\u5bf9\u4e8e\u6570\u636e\u7684\u4f20\u8f93\u4e0e\u5b58\u50a8\u81f3\u5173\u91cd\u8981\u3002\u5c24\u5176\u662f\u5728\u6d89\u53ca\u5230\u4e2d\u6587\u5b57\u7b26\u65f6\uff0cGBK\u4e0eUTF-8\u662f\u4e24\u4e2a\u5e38\u89c1\u7684\u7f16\u7801\u65b9\u5f0f\u3002PHP\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5c06GBK\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aUTF-8\u7f16\u7801\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u4ecb\u7ecd\u51e0\u79cd\u65b9\u6cd5\u4ee5\u53ca\u5982\u4f55\u5177\u4f53\u5b9e\u73b0\u8fd9\u4e9b\u8f6c\u6362\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.idcyz.com\/blog\/wp-content\/uploads\/2025\/07\/XEXK4Y.jpg\" alt=\"PHP\u5b9e\u73b0GBK\u8f6cUTF-8\u7684\u591a\u79cd\u65b9\u6cd5\u4e0e\u5e94\u7528\u5206\u6790\" title=\"PHP\u5b9e\u73b0GBK\u8f6cUTF-8\u7684\u591a\u79cd\u65b9\u6cd5\u4e0e\u5e94\u7528\u5206\u6790\"><\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528mb_convert_encoding\u51fd\u6570<\/h3>\n<\/p>\n<p><p>mb_convert_encoding\u662fPHP\u63d0\u4f9b\u7684\u591a\u5b57\u8282\u5b57\u7b26\u4e32\u51fd\u6570\u4e4b\u4e00\uff0c\u80fd\u591f\u5728\u591a\u79cd\u5b57\u7b26\u7f16\u7801\u95f4\u8fdb\u884c\u8f6c\u6362\u3002\u5b83\u7684\u7528\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u5e76\u4e14\u652f\u6301\u591a\u79cd\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><pre><code><\/p>\r\n<p>$gbk_string = \"\u4f60\u7684GBK\u7f16\u7801\u5b57\u7b26\u4e32\";<\/p>\r\n<p>$utf8_string = mb_convert_encoding($gbk_string, \"UTF-8\", \"GBK\");<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u4e00\u4e2aGBK\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aUTF-8\u7f16\u7801\uff0c\u53ea\u9700\u6307\u5b9a\u6e90\u7f16\u7801\u548c\u76ee\u6807\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528iconv\u51fd\u6570<\/h3>\n<\/p>\n<p><p>iconv\u51fd\u6570\u4e5f\u662f\u4e00\u79cd\u975e\u5e38\u6d41\u884c\u7684\u5b57\u7b26\u7f16\u7801\u8f6c\u6362\u5de5\u5177\uff0c\u5b83\u652f\u6301\u591a\u79cd\u5b57\u7b26\u96c6\u4e4b\u95f4\u7684\u8f6c\u6362\u3002\u4f7f\u7528\u65b9\u6cd5\u540c\u6837\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><pre><code><\/p>\r\n<p>$gbk_string = \"\u4f60\u7684GBK\u7f16\u7801\u5b57\u7b26\u4e32\";<\/p>\r\n<p>$utf8_string = iconv(\"GBK\", \"UTF-8\/\/IGNORE\", $gbk_string);<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u4e86&#8221;\/\/IGNORE&#8221;\u9009\u9879\uff0c\u5b83\u4f1a\u5ffd\u7565\u90a3\u4e9b\u65e0\u6cd5\u8f6c\u6362\u7684\u5b57\u7b26\uff0c\u9002\u5408\u5904\u7406\u53ef\u80fd\u5b58\u5728\u4e71\u7801\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>3. \u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u82e5\u60f3\u8981\u66f4\u7075\u6d3b\u7684\u5904\u7406\u8f6c\u6362\u8fc7\u7a0b\uff0c\u5f00\u53d1\u8005\u4e5f\u53ef\u4ee5\u81ea\u5b9a\u4e49\u8f6c\u6362\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u8ba1\u4e00\u4e2a\u51fd\u6570\u6765\u5904\u7406\u4e00\u4e9b\u7279\u5b9a\u7684\u8f6c\u6362\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code><\/p>\r\n<p>function convert_gbk_to_utf8($gbk_string) {<\/p>\r\n<p>    return mb_convert_encoding($gbk_string, \"UTF-8\", \"GBK\");<\/p>\r\n<p>}<\/p>\r\n<p>$utf8_string = convert_gbk_to_utf8(\"\u4f60\u7684GBK\u7f16\u7801\u5b57\u7b26\u4e32\");<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u51fd\u6570\u7684\u4f7f\u7528\u80fd\u591f\u8ba9\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u53ef\u91cd\u7528\u6027\u3002<\/p>\n<\/p>\n<p><h3>4. \u5b9e\u9645\u5e94\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5c06\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u4eceGBK\u7f16\u7801\u8bfb\u53d6\u5e76\u8f6c\u6362\u4e3aUTF-8\u7f16\u7801\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2aMySQL\u6570\u636e\u5e93\uff0c\u8868\u4e2d\u7684\u5b57\u6bb5\u5b58\u50a8\u7684\u662fGBK\u7f16\u7801\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code><\/p>\r\n<p>$query = \"SELECT gbk_column FROM your_table\";<\/p>\r\n<p>$result = mysqli_query($conn, $query);<\/p>\r\n<p>while ($row = mysqli_fetch_assoc($result)) {<\/p>\r\n<p>    $utf8_string = mb_convert_encoding($row['gbk_column'], \"UTF-8\", \"GBK\");<\/p>\r\n<p>    \/\/ \u5904\u7406$utf8_string<\/p>\r\n<p>}<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u901a\u8fc7\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u6570\u636e\u540e\uff0c\u518d\u8fdb\u884c\u5b57\u7b26\u7f16\u7801\u8f6c\u6362\uff0c\u4f7f\u5f97\u6570\u636e\u80fd\u591f\u5728UTF-8\u73af\u5883\u4e0b\u6b63\u5e38\u663e\u793a\u3002<\/p>\n<\/p>\n<p><h3>\u95ee\u7b54\u73af\u8282<\/h3>\n<\/p>\n<p><b>\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u4e3aGBK\u7f16\u7801\uff1f<\/b><\/p>\n<p><p>\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u5e76\u4e0d\u5bb9\u6613\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u51fd\u6570\u8fdb\u884c\u5c1d\u8bd5\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u5c06\u5176\u8f6c\u6362\u6210UTF-8\u7f16\u7801\uff0c\u5982\u679c\u8f6c\u6362\u540e\u5f97\u5230\u7684\u7ed3\u679c\u5b57\u7b26\u4e32\u957f\u5ea6\u6ca1\u6709\u53d8\u5316\uff0c\u90a3\u5c31\u53ef\u80fd\u4e0d\u662fGBK\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><b>\u4f7f\u7528mb_convert_encoding\u4e0eiconv\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/b><\/p>\n<p><p>mb_convert_encoding\u548ciconv\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\u5904\u7406\u53c2\u6570\u548c\u652f\u6301\u7684\u5b57\u7b26\u96c6\u3002mb_convert_encoding\u76f8\u5bf9\u7b80\u5355\uff0c\u652f\u6301\u7684\u5b57\u7b26\u96c6\u66f4\u5e7f\u6cdb\uff0c\u800ciconv\u5728\u6027\u80fd\u4e0a\u53ef\u80fd\u66f4\u597d\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\u3002<\/p>\n<\/p>\n<p><b>\u5982\u679c\u8f6c\u6362\u540e\u6570\u636e\u4ecd\u7136\u51fa\u73b0\u4e71\u7801\uff0c\u600e\u4e48\u529e\uff1f<\/b><\/p>\n<p><p>\u5982\u679c\u5728\u8f6c\u6362\u540e\u4ecd\u7136\u9047\u5230\u4e71\u7801\uff0c\u53ef\u80fd\u7684\u539f\u56e0\u6709\u5b57\u7b26\u7f16\u7801\u4e0d\u4e00\u81f4\u6216\u6570\u636e\u5728\u5b58\u50a8\u8fc7\u7a0b\u4e2d\u5df2\u88ab\u635f\u574f\u3002\u9996\u5148\u786e\u4fdd\u8bfb\u53d6\u548c\u5b58\u50a8\u7684\u7f16\u7801\u4e00\u81f4\uff0c\u5e76\u5c1d\u8bd5\u5728\u8f6c\u6362\u65f6\u4f7f\u7528&#8221;\/\/IGNORE&#8221;\u9009\u9879\uff0c\u4ee5\u8df3\u8fc7\u4e0d\u80fd\u8f6c\u6362\u7684\u5b57\u7b26\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u73b0\u4ee3\u7684\u7f51\u9875\u5f00\u53d1\u4e2d\uff0c\u5b57\u7b26\u7f16\u7801\u5bf9\u4e8e\u6570\u636e\u7684\u4f20\u8f93\u4e0e\u5b58\u50a8\u81f3\u5173\u91cd\u8981\u3002\u5c24\u5176\u662f\u5728\u6d89\u53ca\u5230\u4e2d\u6587\u5b57\u7b26\u65f6\uff0cGBK\u4e0eUTF-8\u662f\u4e24\u4e2a\u5e38\u89c1\u7684\u7f16\u7801\u65b9\u5f0f\u3002PHP\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5c06GBK\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aUTF-8\u7f16\u7801\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u4ecb\u7ecd\u51e0\u79cd\u65b9\u6cd5\u4ee5\u53ca\u5982\u4f55\u5177\u4f53\u5b9e\u73b0&#8230;<\/p>\n","protected":false},"author":1,"featured_media":680,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[343,345,344],"topic":[],"class_list":["post-679","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jiushu","tag-gbk","tag-utf","tag-344"],"_links":{"self":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts\/679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/comments?post=679"}],"version-history":[{"count":1,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts\/679\/revisions"}],"predecessor-version":[{"id":682,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts\/679\/revisions\/682"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/media\/680"}],"wp:attachment":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/media?parent=679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/categories?post=679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/tags?post=679"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/topic?post=679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}