{"id":220,"date":"2013-08-07T11:00:12","date_gmt":"2013-08-07T03:00:12","guid":{"rendered":"http:\/\/www.zyuns.com\/?p=220"},"modified":"2015-02-15T20:49:41","modified_gmt":"2015-02-15T12:49:41","slug":"php-zhong-iconv-han-shu-shi-yong-fang-fa","status":"publish","type":"post","link":"https:\/\/www.siediyer.cn\/?p=220","title":{"rendered":"php\u4e2diconv\u51fd\u6570\u4f7f\u7528\u65b9\u6cd5"},"content":{"rendered":"<p>\u6700\u8fd1\u5728\u505a\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u9700\u8981\u7528\u5230iconv\u51fd\u6570\u628a\u6293\u53d6\u6765\u8fc7\u7684utf-8\u7f16\u7801\u7684\u9875\u9762\u8f6c\u6210gb2312\uff0c \u53d1\u73b0\u53ea\u6709\u7528iconv\u51fd\u6570\u628a\u6293\u53d6\u8fc7\u6765\u7684\u6570\u636e\u4e00\u8f6c\u7801\u6570\u636e\u5c31\u4f1a\u65e0\u7f18\u65e0\u6545\u7684\u5c11\u4e00\u4e9b\u3002<\/p>\n<p>iconv\u51fd\u6570\u5e93\u80fd\u591f\u5b8c\u6210\u5404\u79cd\u5b57\u7b26\u96c6\u95f4\u7684\u8f6c\u6362\uff0c\u662fphp\u7f16\u7a0b\u4e2d\u4e0d\u53ef\u7f3a\u5c11\u7684\u57fa\u7840\u51fd\u6570\u5e93\u3002<br \/>\n1\u3001\u4e0b\u8f7dlibiconv\u51fd\u6570\u5e93http:\/\/ftp.gnu.org\/pub\/gnu\/libiconv\/libiconv-1.9.2.tar.gz\uff1b<br \/>\n2\u3001\u89e3\u538b\u7f29tar -zxvf libiconv-1.9.2.tar.gz;<br \/>\n3\u3001\u5b89\u88c5libiconv<br \/>\n\uff03configure &#8211;prefix=\/usr\/local\/iconv<br \/>\n#make<br \/>\n#make install<br \/>\n4\u3001\u91cd\u65b0\u7f16\u8bd1php \u589e\u52a0\u7f16\u8bd1\u53c2\u6570&#8211;with-iconv=\/usr\/local\/iconv<\/p>\n<p><strong>windows\u4e0b<br \/>\n<\/strong><br \/>\n\u6700 \u8fd1\u5728\u505a\u4e00\u4e2a\u5c0f\u5077\u7a0b\u5e8f\uff0c\u9700\u8981\u7528\u5230iconv\u51fd\u6570\u628a\u6293\u53d6\u6765\u8fc7\u7684utf-8\u7f16\u7801\u7684\u9875\u9762\u8f6c\u6210gb2312\uff0c \u53d1\u73b0\u53ea\u6709\u7528iconv\u51fd\u6570\u628a\u6293\u53d6\u8fc7\u6765\u7684\u6570\u636e\u4e00\u8f6c\u7801\u6570\u636e\u5c31\u4f1a\u65e0\u7f18\u65e0\u6545\u7684\u5c11\u4e00\u4e9b\u3002 \u8ba9\u6211\u90c1\u95f7\u4e86\u597d\u4e00\u4f1a\u513f\uff0c\u53bb\u7f51\u4e0a\u4e00\u67e5\u8d44\u6599\u624d\u77e5\u9053\u8fd9\u662ficonv\u51fd\u6570\u7684\u4e00\u4e2abug\u3002iconv\u5728\u8f6c\u6362\u5b57\u7b26&#8221;\u2014&#8221;\u5230gb2312\u65f6\u4f1a\u51fa\u9519<br \/>\n\u89e3\u51b3\u65b9\u6cd5\u5f88\u7b80\u5355\uff0c\u5c31\u662f\u5728\u9700\u8981\u8f6c\u6210\u7684\u7f16\u7801\u540e\u52a0 &#8220;\/\/IGNORE&#8221; \u4e5f\u5c31\u662ficonv\u51fd\u6570\u7b2c\u4e8c\u4e2a\u53c2\u6570\u540e.\u5982\u4e0b\uff1a<\/p>\n<p>\u4ee5\u4e0b\u4e3a\u5f15\u7528\u7684\u5185\u5bb9\uff1a<\/p>\n<pre class=\"wrap:true lang:default decode:true \">iconv(\"UTF-8\",\"GB2312\/\/IGNORE\",$data)<\/pre>\n<p>ignore\u7684\u610f\u601d\u662f\u5ffd\u7565\u8f6c\u6362\u65f6\u7684\u9519\u8bef\uff0c\u5982\u679c\u6ca1\u6709ignore\u53c2\u6570\uff0c\u6240\u6709\u8be5\u5b57\u7b26\u540e\u9762\u7684\u5b57\u7b26\u4e32\u90fd\u65e0\u6cd5\u88ab\u4fdd\u5b58\u3002<\/p>\n<pre class=\"wrap:true lang:default decode:true \">&lt;?php\necho $str= '\u4f60\u597d,\u8fd9\u91cc\u662f\u5356\u5496\u5561!';\necho '&lt;br \/&gt;';\necho iconv('GB2312', 'UTF-8', $str); \/\/\u5c06\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u4eceGB2312\u8f6c\u5230UTF-8\necho '&lt;br \/&gt;';\necho iconv_substr($str, 1, 1, 'UTF-8'); \/\/\u6309\u5b57\u7b26\u4e2a\u6570\u622a\u53d6\u800c\u975e\u5b57\u8282\nprint_r(iconv_get_encoding()); \/\/\u5f97\u5230\u5f53\u524d\u9875\u9762\u7f16\u7801\u4fe1\u606f\necho iconv_strlen($str, 'UTF-8'); \/\/\u5f97\u5230\u8bbe\u5b9a\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u957f\u5ea6\n\/\/\u4e5f\u6709\u8fd9\u6837\u7528\u7684\n$content = iconv(\"UTF-8\",\"gbk\/\/TRANSLIT\",$content);\n?&gt;<\/pre>\n<p>iconv\u4e0d\u662fphp\u7684\u9ed8\u8ba4\u51fd\u6570\uff0c\u4e5f\u662f\u9ed8\u8ba4\u5b89\u88c5\u7684\u6a21\u5757\u3002\u9700\u8981\u5b89\u88c5\u624d\u80fd\u7528\u7684\u3002<br \/>\n\u5982\u679c\u662fwindows2000+php\uff0c\u4f60\u53ef\u4ee5\u4fee\u6539php.ini\u6587 \u4ef6\uff0c\u5c06extension=php_iconv.dll\u524d\u7684&#8221;;&#8221;\u53bb\u6389\uff0c\u540c\u65f6\u4f60\u8981copy\u4f60\u7684\u539fphp\u5b89\u88c5\u6587\u4ef6\u4e0b\u7684iconv.dll\u5230\u4f60\u7684 winnt\/system32\u4e0b\uff08\u5982\u679c\u4f60\u7684dll\u6307\u5411\u7684\u662f\u8fd9\u4e2a\u76ee\u5f55\uff09<br \/>\n\u5728linux\u73af\u5883\u4e0b\uff0c\u7528\u9759\u6001\u5b89\u88c5\u7684\u65b9\u5f0f\uff0c\u5728configure\u65f6\u52a0\u591a\u4e00\u9879 &#8211;with-iconv\u5c31\u53ef\u4ee5\u4e86\uff0cphpinfo\u770b\u5f97\u5230iconv\u7684\u9879\u3002(Linux7.3+Apache4.06+php4.3.2)\uff0c<\/p>\n<p>\u4e0b\u8f7d\uff1aftp:\/\/ftp.gnu.org\/pub\/gnu\/libiconv\/libiconv-1.8.tar.gz<br \/>\n\u5b89\u88c5\uff1a<br \/>\n#cp libiconv-1.8.tar.gz \/usr\/local\/src<br \/>\n#tar zxvf lib*<br \/>\n#.\/configure &#8211;prefix=\/usr\/local\/libiconv<br \/>\n#make<br \/>\n#make install<br \/>\n\u7f16\u8bd1php<br \/>\n#.\/configure &#8211;prefix=\/usr\/local\/php4.3.2 &#8211;with-iconv=\/usr\/local\/libiconv\/<br \/>\n<strong>\u4f7f\u7528\u7684\u7b80\u5355\u4f8b\u5b50\uff1a<br \/>\n<\/strong><br \/>\n&lt;?php<br \/>\necho iconv(&#8220;gb2312&#8243;,&#8221;ISO-8859-1&#8243;,&#8221;\u6211\u4eec&#8221;);<br \/>\n?&gt;<\/p>\n<p><strong>PHP\u4e2d\u7684mb_convert_encoding\u4e0eiconv\u51fd\u6570\u4ecb\u7ecd<\/p>\n<p><\/strong>mb_convert_encoding\u8fd9\u4e2a\u51fd\u6570\u662f\u7528\u6765\u8f6c\u6362\u7f16\u7801\u7684\u3002\u539f\u6765\u4e00\u76f4\u5bf9\u7a0b\u5e8f\u7f16\u7801\u8fd9\u4e00\u6982\u5ff5\u4e0d\u7406\u89e3\uff0c\u4e0d\u8fc7\u73b0\u5728\u597d\u50cf\u6709\u70b9\u5f00\u7a8d\u4e86\u3002<br \/>\n\u4e0d\u8fc7\u82f1\u6587\u4e00\u822c\u4e0d\u4f1a\u5b58\u5728\u7f16\u7801\u95ee\u9898\uff0c\u53ea\u6709\u4e2d\u6587\u6570\u636e\u624d\u4f1a\u6709\u8fd9\u4e2a\u95ee\u9898\u3002\u6bd4\u5982\u4f60\u7528Zend Studio\u6216Editplus\u5199\u7a0b\u5e8f\u65f6\uff0c\u7528\u7684\u662fgbk\u7f16\u7801\uff0c\u5982\u679c\u6570\u636e\u9700\u8981\u5165\u6570\u636e\u5e93\uff0c\u800c\u6570\u636e\u5e93\u7684\u7f16\u7801\u4e3autf8\u65f6\uff0c\u8fd9\u65f6\u5c31\u8981\u628a\u6570\u636e\u8fdb\u884c\u7f16\u7801\u8f6c\u6362\uff0c\u4e0d\u7136\u8fdb\u5230\u6570\u636e\u5e93\u5c31\u4f1a\u53d8\u6210\u4e71\u7801\u3002<\/p>\n<p>mb_convert_encoding\u7684\u7528\u6cd5\u89c1\u5b98\u65b9\uff1a<br \/>\nhttp:\/\/cn.php.net\/manual\/zh\/function.mb-convert-encoding.php<\/p>\n<p>\u505a\u4e00\u4e2aGBK To UTF-8<br \/>\n&lt; ?php<br \/>\nheader(&#8220;content-Type: text\/html; charset=Utf-8&#8221;);<br \/>\necho mb_convert_encoding(&#8220;\u59b3\u4fc2\u6211\u7684\u53cb\u4ed4&#8221;, &#8220;UTF-8&#8221;, &#8220;GBK&#8221;);<br \/>\n?&gt;<\/p>\n<p>\u518d\u6765\u4e2aGB2312 To Big5<br \/>\n&lt; ?php<br \/>\nheader(&#8220;content-Type: text\/html; charset=big5&#8221;);<br \/>\necho mb_convert_encoding(&#8220;\u4f60\u662f\u6211\u7684\u670b\u53cb&#8221;, &#8220;big5&#8221;, &#8220;GB2312&#8221;);<br \/>\n?&gt;<br \/>\n\u4e0d\u8fc7\u8981\u4f7f\u7528\u4e0a\u9762\u7684\u51fd\u6570\u9700\u8981\u5b89\u88c5\u4f46\u662f\u9700\u8981\u5148enable mbstring \u6269\u5c55\u5e93\u3002<\/p>\n<p>PHP\u4e2d\u7684\u53e6\u5916\u4e00\u4e2a\u51fd\u6570iconv\u4e5f\u662f\u7528\u6765\u8f6c\u6362\u5b57\u7b26\u4e32\u7f16\u7801\u7684\uff0c\u4e0e\u4e0a\u51fd\u6570\u529f\u80fd\u76f8\u4f3c\u3002<\/p>\n<p>\u4e0b\u9762\u8fd8\u6709\u4e00\u4e9b\u8be6\u7ec6\u7684\u4f8b\u5b50\uff1a<br \/>\niconv \u2014 Convert string to requested character encoding<br \/>\n(PHP 4 &gt;= 4.0.5, PHP 5)<br \/>\nmb_convert_encoding \u2014 Convert character encoding<br \/>\n(PHP 4 &gt;= 4.0.6, PHP 5)<\/p>\n<p>\u7528\u6cd5\uff1a<br \/>\nstring mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] )<br \/>\n\u9700\u8981\u5148enable mbstring \u6269\u5c55\u5e93\uff0c\u5728 php.ini\u91cc\u5c06; extension=php_mbstring.dll \u524d\u9762\u7684 ; \u53bb\u6389<br \/>\nmb_convert_encoding \u53ef\u4ee5\u6307\u5b9a\u591a\u79cd\u8f93\u5165\u7f16\u7801\uff0c\u5b83\u4f1a\u6839\u636e\u5185\u5bb9\u81ea\u52a8\u8bc6\u522b,\u4f46\u662f\u6267\u884c\u6548\u7387\u6bd4iconv\u5dee\u592a\u591a\uff1b<\/p>\n<p>string iconv ( string in_charset, string out_charset, string str )<br \/>\n\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<br \/>\nReturns the converted string or FALSE on failure.<\/p>\n<p>\u4f7f\u7528\uff1a<\/p>\n<p>\u53d1\u73b0iconv\u5728\u8f6c\u6362\u5b57\u7b26\u201d\u2014\u201d\u5230gb2312\u65f6\u4f1a\u51fa\u9519\uff0c\u5982\u679c\u6ca1\u6709ignore\u53c2\u6570\uff0c\u6240\u6709\u8be5\u5b57\u7b26\u540e\u9762\u7684\u5b57\u7b26\u4e32\u90fd\u65e0\u6cd5\u88ab\u4fdd\u5b58\u3002\u4e0d\u7ba1\u600e\u4e48\u6837\uff0c\u8fd9\u4e2a\u201d\u2014\u201d\u90fd\u65e0\u6cd5\u8f6c\u6362\u6210\u529f\uff0c\u65e0\u6cd5\u8f93\u51fa\u3002 \u53e6\u5916mb_convert_encoding\u6ca1\u6709\u8fd9\u4e2abug.<\/p>\n<p>\u4e00\u822c\u60c5\u51b5\u4e0b\u7528 iconv\uff0c\u53ea\u6709\u5f53\u9047\u5230\u65e0\u6cd5\u786e\u5b9a\u539f\u7f16\u7801\u662f\u4f55\u79cd\u7f16\u7801\uff0c\u6216\u8005iconv\u8f6c\u5316\u540e\u65e0\u6cd5\u6b63\u5e38\u663e\u793a\u65f6\u624d\u7528mb_convert_encoding \u51fd\u6570.<\/p>\n<p>from_encoding is specified by character code name before conversion. it can be array or string &#8211; comma separated enumerated list. If it is not specified, the internal encoding will be used.<br \/>\n\/* Auto detect encoding from JIS, eucjp-win, sjis-win, then convert str to UCS-2LE *\/<br \/>\n$str = mb_convert_encoding($str, \u201cUCS-2LE\u201d, \u201cJIS, eucjp-win, sjis-win\u201d);<br \/>\n\/* \u201cauto\u201d is expanded to \u201cASCII,JIS,UTF-8,EUC-JP,SJIS\u201d *\/<br \/>\n$str = mb_convert_encoding($str, \u201cEUC-JP\u201d, \u201cauto\u201d);<\/p>\n<p>\u4f8b\u5b50\uff1a<br \/>\n$content = iconv(\u201dGBK\u201d, \u201cUTF-8\u2033, $content);<br \/>\n$content = mb_convert_encoding($content, &#8220;UTF-8\u2033,&#8221;GBK&#8221;);<\/p>\n<p><strong>php\u4e2d\u4f7f\u7528iconv\u51fd\u6570\u65f6\u5bb9\u6613\u5ffd\u7565\u7684\u53c2\u6570<br \/>\n<\/strong>\u4eca \u5929\u5728\u5904\u7406\u6293\u53d6\u5185\u5bb9\u7684\u65f6\u5019\uff0c\u5f53\u91c7\u7528iconv\u8fdb\u884c\u7f16\u7801\u8f6c\u6362\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u7ed3\u679c\u4f1a\u4e2d\u65ad\uff0c\u731c\u662f\u5b57\u7b26\u96c6\u7684\u95ee\u9898\uff0c\u8003\u8651\u600e\u4e48\u8df3\u8fc7\u76ee\u6807\u5b57\u7b26\u96c6\u4e0d\u5b58\u5728\u7684\u5b57\u7b26\uff0c\u67e5\u624b\u518c\u53d1\u73b0 iconv\u7684\u51fd\u6570\u53ea\u6709\u4e09\u4e2a\u53c2\u6570\uff0c\u597d\u50cf\u4e0d\u884c\uff0c\u7136\u540e\u67e5\u7f51\u4e0a\u6709\u4eba\u8bf4\u53ef\u4ee5\uff0c\u4f46\u662f\u5f88\u5947\u602a\u600e\u4e48\u5b9e\u73b0\uff0c\u6700\u540e\u53d1\u73b0\u82f1\u6587\u63cf\u8ff0\u6709\u8bf4\u53ef\u4ee5\u52a0\u6807\u8bc6\u5230\u76ee\u6807\u7f16\u7801\u540e \u9762\uff1a\u201cTRANSLIT\u201d\uff0c\u5f88\u90c1\u95f7\u600e\u4e48\u52a0\u5462\uff1f\u539f\u6765\u662f\u5148\u52a0\u201c\/\/\u201d\uff0c\u771f\u662f\u90c1\u95f7\uff0c\u7adf\u7136\u6709\u8fd9\u6837\u7684\u8bbe\u8ba1<br \/>\n\u539f\u578b\uff1a $txtContent = iconv(&#8220;utf-8&#8221;,&#8217;GBK&#8217;,$txtContent);<\/p>\n<p>\u7279\u6b8a\u53c2\u6570\uff1aiconv(&#8220;UTF-8&#8243;,&#8221;GB2312\/\/IGNORE&#8221;,$data)<\/p>\n<p>\u4e24\u4e2a\u53ef\u9009\u7684\u8f85\u52a9\u53c2\u6570\uff1aTRANSLIT\u548cIGNORE \uff0c(\u5176\u4e2dIGNORE \u5c31\u662f\u8bf4\u9047\u5230\u65e0\u6cd5\u8f6c\u6362\u7684\u5c31\u8df3\u8fc7)\u3002 Description<\/p>\n<p>string iconv ( string in_charset, string out_charset, string str )<\/p>\n<p>Performs a character set conversion on the string str from in_charset to out_charset. Returns the converted string or FALSE on failure.<\/p>\n<p>If you append the string \/\/TRANSLIT to out_charset transliteration is activated. This means that when a character can&#8217;t be represented in the target charset, it can be approximated through one or several similarly looking characters. If you append the string \/\/IGNORE, characters that cannot be represented in the target charset are silently discarded. Otherwise, str is cut from the first illegal character<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u505a\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u9700\u8981\u7528\u5230iconv\u51fd\u6570\u628a\u6293\u53d6\u6765\u8fc7\u7684utf-8\u7f16\u7801\u7684\u9875\u9762\u8f6c\u6210gb2312\uff0c \u53d1\u73b0\u53ea\u6709\u7528icon [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-220","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=220"}],"version-history":[{"count":1,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/220\/revisions"}],"predecessor-version":[{"id":436,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/220\/revisions\/436"}],"wp:attachment":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}