{"id":211,"date":"2008-10-10T13:07:21","date_gmt":"2008-10-09T21:07:21","guid":{"rendered":"http:\/\/blog.longwin.com.tw\/?p=211"},"modified":"2008-10-10T13:07:21","modified_gmt":"2008-10-09T21:07:21","slug":"php-iconv","status":"publish","type":"post","link":"https:\/\/blog.fhl.net\/?p=211","title":{"rendered":"php\u7684 iconv \u7e41\u7c21\u8f49\u63db\u6587\u5b57\u4e0d\u51fa\u4f86\u554f\u984c\u89e3\u6cd5"},"content":{"rendered":"<p>\nphp\u7684iconv\u662f\u500b\u975e\u5e38\u597d\u7528\u7684\u51fd\u6578\uff0c\u53ef\u4ee5\u8f15\u6613\u9054\u6210\u7e41\u7c21\u9ad4\u8f49\u78bc\u7684\u529f\u80fd\uff0c \u4f46\u662f\u9019\u500b\u51fd\u6578\u6709\u500b\u7f3a\u9ede\uff0c\u7576\u6211\u5011\u8f38\u5165\u300c\u3010\u56e7\u7537\u5b69\u3011\u70ba\u4ec0\u9ebc\u4f60\u8981\u4e00\u76f4\u7b11\u5462\uff1f\u300d\u9019\u6bb5\u5b57\uff0c\u7531\u65bc \u300c\u56e7\u300d\u9019\u500b\u5b57\u4e0d\u662fgb2312\u652f\u63f4\u7684\u5b57\uff0c\u7d50\u679c\u6574\u500b\u8f38\u51fa\u6703\u8b8a\u6210\u300c\u3010\u300d\uff0c\u5f8c\u9762\u7684\u5b57\u4e32\u5168\u90e8\u6703\u6d88\u5931\uff0c\u800c\u4e14 php\u7aef\u6c92\u6709\u4efb\u4f55\u932f\u8aa4\u4fe1\u606f\uff08return\u4e5f\u4e0d\u6703\u8b8a\u6210 false)\u3002\n<\/p>\n<p>\n\u70ba\u4e86\u89e3\u6c7a\u9019\u500b\u554f\u984c\uff0c\u6211\u7528\u4e86\u8f49\u56de\u4f86\u7684\u5b57\u4e32\u9577\u5ea6\u4f5c\u70ba\u6aa2\u9a57\u6a19\u7aff\uff0c\u770b\u770b\u8f49\u56de\u4f86\u7684\u5b57\u4e32\u662f\u5426\u6b63\u78ba\uff0c\u5982\u679c\u4e0d\u6b63\u78ba\uff0c\u5c31\u7528\u4e8c\u5206\u903c\u8fd1\u6cd5\uff0c\u5148\u628a\u6b63\u78ba\u7684\u90e8\u4efd\u8f49\u51fa\u4f86\uff0c\u628a\u4e0d\u6b63\u78ba\u7684\u90e8\u4efd\u7528\u905e\u8ff4\u65b9\u5f0f\u7e7c\u7e8c\u5207\u5272\u4e0b\u53bb\u8f49\uff0c\u6700\u5f8c\u628a\u90a3\u500b\u9020\u6210\u554f\u984c\u7684\u5b57\u63db\u6210\u300c?\u300d\uff0c\u9019\u6a23\u5c31\u89e3\u6c7a\u4e86\u554f\u984c\u3002\n<\/p>\n<p>\n&nbsp;&lt;?php<br \/>\nfunction trangb($str)<br \/>\n&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $nstr=iconv(&#8216;utf-8&#8242;,&#8217;big5&#8217;,$str);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (iconv_strlen($str,&#8217;utf-8&#8242;)!=iconv_strlen($nstr,&#8217;big5&#8242;))<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $nstr=riconv(&#8216;utf-8&#8242;,&#8217;big5&#8217;,$str);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $pnstr=iconv(&#8216;big5&#8242;,&#8217;gb2312&#8217;,$nstr);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (iconv_strlen($nstr,&#8217;big5&#8242;)!=iconv_strlen($pnstr,&#8217;gb2312&#8242;))<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $pnstr=riconv(&#8216;big5&#8242;,&#8217;gb2312&#8217;,$nstr);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $nstr= iconv(&#8216;gb2312&#8242;,&#8217;utf-8&#8217;,$pnstr);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return $nstr;<br \/>\n&nbsp;}<br \/>\nfunction riconv($loc1,$loc2,$str)<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; $i=iconv_strlen($str,$loc1);<br \/>\n&nbsp;&nbsp;&nbsp; if ($i&lt;=1)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return &#8216;?&#8217;;<br \/>\n&nbsp;&nbsp;&nbsp; $blen=(int)($i\/2);<br \/>\n&nbsp;&nbsp;&nbsp; $elen=$i-$blen;<br \/>\n&nbsp;&nbsp;&nbsp; $bstr=iconv_substr($str,0,$blen,$loc1);<br \/>\n&nbsp;&nbsp;&nbsp; $nbstr=iconv($loc1,$loc2,$bstr);<br \/>\n&nbsp;&nbsp;&nbsp; if (iconv_strlen($bstr,$loc1)!=iconv_strlen($nbstr,$loc2))<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $nbstr=riconv($loc1,$loc2,$bstr);<br \/>\n&nbsp;&nbsp;&nbsp; $estr=iconv_substr($str,$blen,$elen,$loc1);<br \/>\n&nbsp;&nbsp;&nbsp; $nestr=iconv($loc1,$loc2,$estr);<br \/>\n&nbsp;&nbsp;&nbsp; if (iconv_strlen($estr,$loc1)!=iconv_strlen($nestr,$loc2))<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $nestr=riconv($loc1,$loc2,$estr);<br \/>\n&nbsp;&nbsp;&nbsp; return $nbstr.$nestr;<br \/>\n}<br \/>\necho trangb(&quot;\u3010\u56e7\u7537\u5b69\u3011\u70ba\u4ec0\u9ebc\u4f60\u8981\u4e00\u76f4\u7b11\u5462\uff1f&quot;);<br \/>\n?&gt;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php\u7684iconv\u662f\u500b\u975e\u5e38\u597d\u7528\u7684\u51fd\u6578\uff0c\u53ef\u4ee5\u8f15\u6613\u9054\u6210\u7e41\u7c21\u9ad4\u8f49\u78bc\u7684\u529f\u80fd\uff0c \u4f46\u662f\u9019\u500b\u51fd\u6578\u6709\u500b\u7f3a\u9ede\uff0c\u7576\u6211\u5011\u8f38\u5165\u300c\u3010\u56e7\u7537 &hellip; <a href=\"https:\/\/blog.fhl.net\/?p=211\" class=\"more-link\">\u95b1\u8b80\u5168\u6587 <span class=\"screen-reader-text\">php\u7684 iconv \u7e41\u7c21\u8f49\u63db\u6587\u5b57\u4e0d\u51fa\u4f86\u554f\u984c\u89e3\u6cd5<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-211","post","type-post","status-publish","format-standard","hentry","category-e"],"_links":{"self":[{"href":"https:\/\/blog.fhl.net\/index.php?rest_route=\/wp\/v2\/posts\/211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.fhl.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.fhl.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.fhl.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.fhl.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=211"}],"version-history":[{"count":0,"href":"https:\/\/blog.fhl.net\/index.php?rest_route=\/wp\/v2\/posts\/211\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.fhl.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.fhl.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.fhl.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}