<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[[iconv mb_convert_encoding]PHP上传文件时中文出现乱码解决方案！]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Thu, 29 May 2008 03:45:26 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	&nbsp;&nbsp;function set_upload_data($arr_upload)<br/>&nbsp;&nbsp;&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;attachment=$arr_upload[files];<br/>&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;upload_dir=$arr_upload[&quot;upload_dir&quot;];<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;function uploadfile()<br/>&nbsp;&nbsp;&#123;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;$fileName = mb_convert_encoding($this-&gt;attachment[&#039;name&#039;],&#039;GBK&#039;,&#039;UTF-8&#039;);//这一行开始转码，否则出现乱码<br/>&nbsp;&nbsp; //$fileName = iconv(&quot;UTF-8&quot;,&quot;GBK&quot;, $this-&gt;attachment[&#039;name&#039;]);&nbsp;&nbsp;//这样也行，由UTF-8转到GBK,和上面的函数有区别<br/>&nbsp;&nbsp;&nbsp;&nbsp;if(move_uploaded_file($this-&gt;attachment[&#039;tmp_name&#039;],$this-&gt;upload_dir.$fileName))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#123;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 1;//上传成功<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;else&#123;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;//上传失败<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&#125;<br/><br/>参考如下文章：<br/>mb_convert_encoding这个函数是用来转换编码的。原来一直对程序编码这一概念不理解，不过现在好像有点开窍了。<br/>不过英文一般不会存在编码问题，只有中文数据才会有这个问题。比如你用Zend Studio或Editplus写程序时，用的是gbk编码，如果数据需要入数据库，而数据库的编码为utf8时，这时就要把数据进行编码转换，不然进到数据库就会变成乱码。<br/><br/>mb_convert_encoding的用法见官方：<br/>http://cn.php.net/manual/zh/function.mb-convert-encoding.php<br/><br/>做一个GBK To UTF-8 <br/>&lt; ?php <br/>header(&quot;content-Type: text/html; charset=Utf-8&quot;); <br/>echo mb_convert_encoding(&quot;妳係我的友仔&quot;, &quot;UTF-8&quot;, &quot;GBK&quot;); <br/>?&gt; <br/><br/>再来个GB2312 To Big5 <br/>&lt; ?php <br/>header(&quot;content-Type: text/html; charset=big5&quot;); <br/>echo mb_convert_encoding(&quot;你是我的朋友&quot;, &quot;big5&quot;, &quot;GB2312&quot;); <br/>?&gt; <br/>不过要使用上面的函数需要安装但是需要先enable mbstring 扩展库。<br/><br/>PHP中的另外一个函数iconv也是用来转换字符串编码的，与上函数功能相似。<br/><br/>下面还有一些详细的例子：<br/>iconv — Convert string to requested character encoding<br/>(PHP 4 &gt;= 4.0.5, PHP 5)<br/>mb_convert_encoding — Convert character encoding<br/>(PHP 4 &gt;= 4.0.6, PHP 5)<br/><br/>用法：<br/>string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] )<br/>需要先enable mbstring 扩展库，在 php.ini里将; extension=php_mbstring.dll 前面的 ; 去掉<br/>mb_convert_encoding 可以指定多种输入编码，它会根据内容自动识别,但是执行效率比iconv差太多；<br/><br/><br/>string iconv ( string in_charset, string out_charset, string str )<br/>注意：第二个参数，除了可以指定要转化到的编码以外，还可以增加两个后缀：//TRANSLIT 和 //IGNORE，其中 //TRANSLIT 会自动将不能直接转化的字符变成一个或多个近似的字符，//IGNORE 会忽略掉不能转化的字符，而默认效果是从第一个非法字符截断。<br/>Returns the converted string or FALSE on failure.<br/><br/><br/>使用：<br/><br/>发现iconv在转换字符”—”到gb2312时会出错，如果没有ignore参数，所有该字符后面的字符串都无法被保存。不管怎么样，这个”—”都无法转换成功，无法输出。 另外mb_convert_encoding没有这个bug.<br/><br/>一般情况下用 iconv，只有当遇到无法确定原编码是何种编码，或者iconv转化后无法正常显示时才用mb_convert_encoding 函数.<br/><br/>from_encoding is specified by character code name before conversion. it can be array or string - comma separated enumerated list. If it is not specified, the internal encoding will be used.<br/>/* Auto detect encoding from JIS, eucjp-win, sjis-win, then convert str to UCS-2LE */<br/>$str = mb_convert_encoding($str, “UCS-2LE”, “JIS, eucjp-win, sjis-win”);<br/>/* “auto” is expanded to “ASCII,JIS,UTF-8,EUC-JP,SJIS” */<br/>$str = mb_convert_encoding($str, “EUC-JP”, “auto”);<br/><br/>例子：<br/>$content = iconv(”GBK”, “UTF-8″, $content);<br/>$content = mb_convert_encoding($content, “UTF-8″, “GBK”);<br/><br/>如果前端是GBK，而在生成Json时需要做转换才能中文正常显示，如下（把GBK转为Utf8）：<br/><textarea name="code" class="php" rows="15" cols="100">
&nbsp;&nbsp;if((is_array($needZkClientIps))&amp;&amp;(count($needZkClientIps)&gt;=1))
&nbsp;&nbsp;&#123;//只要有一个没有重装的IP存在，则提示给前端用户，不再往下执行。
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$msg = &quot;发现以下IP没有安装Zk，请安后再做分配操作：&quot;.join(&quot;;&quot;, $needZkClientIps);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$arrOut = array(&quot;code&quot;=&gt;&quot;-11&quot;,&quot;msg&quot;=&gt; iconv(&quot;GBK&quot;, &quot;UTF-8&quot;, $msg));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$jsonOutStr = json_encode($arrOut);
&nbsp;&nbsp;&nbsp;&nbsp;die($jsonOutStr);
&nbsp;&nbsp;&#125;&nbsp;&nbsp;
</textarea><br/>最后：Ajax走的都是Utf8，所以得从GBK转为Utf8.
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [iconv mb_convert_encoding]PHP上传文件时中文出现乱码解决方案！]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>