免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1957 | 回复: 9
打印 上一主题 下一主题

急,辛苦一晚没解决的== 中文乱码问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-10-11 00:35 |只看该作者 |倒序浏览
可能我的方法有问题.

要从数据库里需数据,通过这些数据生成xml文件.
我是通过循环生成一个字符串,然后写入到一个文件里

但是得到的文件在浏览器里有时打不开,有时可以直接打开
通过mb_detect_encoding($str)检测到字符串是ASCII 的,通过iconv()和mb_convert_encoding()和utf8_encode()转换过,转换后可以在浏览器中打开时,其中的  中文  是乱码,


  1. $content = '<?xml version="1.0"?>'."\n";
  2. $content.="<data>\n";

  3. $sql = "select * from parts where parts_sn in(select Sn from Daiguan where sellers_id = ".$seller_id.") order by parts_sn";
  4. $DB->query($sql);
  5. $data = $DB->Get_Rows_Array();
  6.        
  7. foreach($data as $row)
  8. {
  9. $content .="<node>\n";
  10.         $content .='<parts property="sn">'.$row['parts_sn']."</parts>\n";
  11.         $content .='<parts property="oldno">'.$row['parts_old_No']."</parts>\n";
  12.         $content .='<parts property="name">'.$row['parts_name']."</parts>\n";
  13.         $content .='<parts property="type">'.$row['parts_type']."</parts>\n";
  14.         $content .='<parts property="note">'.$row['parts_notes_i']."</parts>\n";
  15.         $content .='<parts property="no1">'.$row['parts_No_1']."</parts>\n";
  16.         $content .='<parts property="no2">'.$row['parts_No_2']."</parts>\n";
  17.         $content .='<parts property="no3">'.$row['parts_No_3']."</parts>\n";
  18.         $content .='<parts property="unit">'.$row['parts_unit']."</parts>\n";
  19. $content .="</node>\n";
  20. }
  21. $content.="</data>";

  22. fwrite($file_handle,$content);
复制代码

[ 本帖最后由 LiveHappy 于 2006-10-11 00:52 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-10-11 00:38 |只看该作者
下面这种写法也不行


  1. $content1 = '<?xml version="1.0"?>'."\n";
  2. $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);

  3. $content1="<data>\n";
  4. $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);

  5. $sql = "select * from parts where parts_sn in(select Sn from Daiguan where sellers_id = ".$seller_id.") order by parts_sn";
  6. $DB->query($sql);
  7. $data = $DB->Get_Rows_Array();

  8. foreach($data as $row)
  9. {
  10. $content1="<node>\n";
  11. $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);

  12.         $content1='<parts property="sn">';
  13.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);

  14.         $content1=$row['parts_sn'];
  15.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  16.         $content1="</parts>\n";
  17.         $content1='<parts property="oldno">';
  18.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  19.         $content1=$row['parts_old_No'];
  20.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  21.         $content1="</parts>\n";
  22.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  23.         $content1=='<parts property="name">';
  24.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  25.         $content1=$row['parts_name'];
  26.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  27.         $content1="</parts>\n";
  28.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  29.         $content1=='<parts property="type">';
  30.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  31.         $content1=$row['parts_type'];       
  32.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  33.         $content1="</parts>\n";
  34.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  35.         $content1='<parts property="note">';
  36.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  37.         $content1=$row['parts_notes_i'];
  38.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  39.         $content1="</parts>\n";
  40.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  41.         $content1='<parts property="no1">';
  42.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  43.         $content1=$row['parts_No_1'];
  44.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  45.         $content1="</parts>\n";
  46.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  47.         $content1='<parts property="no2">';
  48.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  49.         $content1=$row['parts_No_2'];
  50.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  51.         $content1="</parts>\n";
  52.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  53.         $content1='<parts property="no3">';
  54.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  55.         $content1=$row['parts_No_3'];
  56.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  57.         $content1="</parts>\n";
  58.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  59.         $content1='<parts property="unit">';
  60.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  61.         $content1=$row['parts_unit'];
  62.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  63.         $content1="</parts>\n";
  64.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  65. $content1="</node>\n";
  66. $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  67. }
  68. $content1="</data>";
  69. $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
复制代码

论坛徽章:
0
3 [报告]
发表于 2006-10-11 00:43 |只看该作者
把iconv()换成mb_convert_encoding()也不行;
使用utf8_encode()也是不行的.
用EditPlus打开是没问题的,但是在浏览器中打开时,中文就是乱码.
如果不转换成utf8,浏览器就无法直接打开.

  1. $content = '<?xml version="1.0"?>'."\n";
  2. $content.="<data>\n";

  3. $sql = "select * from parts where parts_sn in(select Sn from Daiguan where sellers_id = ".$seller_id.") order by parts_sn";
  4. $DB->query($sql);
  5. $data = $DB->Get_Rows_Array();
  6.        
  7. foreach($data as $row)
  8. {
  9. $content .="<node>\n";
  10.         $content .='<parts property="sn">'.$row['parts_sn']."</parts>\n";
  11.         $content .='<parts property="oldno">'.$row['parts_old_No']."</parts>\n";
  12.         $content .='<parts property="name">'.$row['parts_name']."</parts>\n";
  13.         $content .='<parts property="type">'.$row['parts_type']."</parts>\n";
  14.         $content .='<parts property="note">'.$row['parts_notes_i']."</parts>\n";
  15.         $content .='<parts property="no1">'.$row['parts_No_1']."</parts>\n";
  16.         $content .='<parts property="no2">'.$row['parts_No_2']."</parts>\n";
  17.         $content .='<parts property="no3">'.$row['parts_No_3']."</parts>\n";
  18.         $content .='<parts property="unit">'.$row['parts_unit']."</parts>\n";
  19. $content .="</node>\n";
  20. }
  21. $content.="</data>";


  22. fwrite($file_handle,utf8_encode($content));
复制代码

论坛徽章:
0
4 [报告]
发表于 2006-10-11 00:51 |只看该作者
下面是没有转换,在浏览器里直接打开的效果
其中的name应该是中文

  1. <?xml version="1.0" ?>
  2. - <data>
  3. - <node>
  4.   <parts property="sn">2721</parts>
  5.   <parts property="oldno">986006</parts>
  6.   <parts property="name">&raquo;·&sup1;&iquest;</parts>
  7.   <parts property="type">51-70</parts>
  8.   <parts property="note" />
  9.   <parts property="no1">Q67570</parts>
  10.   <parts property="no2">0</parts>
  11.   <parts property="no3" />
  12.   <parts property="unit">&cedil;&ouml;</parts>
  13.   </node>
  14.   </data>
复制代码

在EditPlus里打开是完整的


  1. <?xml version="1.0"?>
  2. <data>
  3. <node>
  4. <parts property="sn">2721</parts>
  5. <parts property="oldno">986006</parts>
  6. <parts property="name">环箍</parts>
  7. <parts property="type">51-70</parts>
  8. <parts property="note"> </parts>
  9. <parts property="no1">Q67570</parts>
  10. <parts property="no2">0</parts>
  11. <parts property="no3"> </parts>
  12. <parts property="unit">个</parts>
  13. </node>
  14. </data>
复制代码

不知道有什么办法,可以不需要转换编码,直接生成xml文件.

论坛徽章:
0
5 [报告]
发表于 2006-10-11 00:52 |只看该作者

论坛徽章:
0
6 [报告]
发表于 2006-10-11 06:46 |只看该作者
应该声明一下xml文件的编码
<?xml version="1.0" encoding="utf-8" ?>

另外mb_detect_encoding并不是很准确。

论坛徽章:
0
7 [报告]
发表于 2006-10-11 08:55 |只看该作者
楼主你连接数据库后有没有
set charset utf8;

论坛徽章:
0
8 [报告]
发表于 2006-10-11 09:46 |只看该作者
原帖由 achun.shx 于 2006-10-11 08:55 发表
楼主你连接数据库后有没有
set charset utf8;


我用的是ms sql 应该不用set吧

论坛徽章:
0
9 [报告]
发表于 2006-10-11 09:49 |只看该作者
原帖由 legend9 于 2006-10-11 06:46 发表
应该声明一下xml文件的编码
<?xml version="1.0" encoding="utf-8" ?>

另外mb_detect_encoding并不是很准确。



加了声明encoding="utf-8"后可以打开,中文可以正确显示,但是有些显示的出来,有些显示不出来.
代码如下

  1. foreach($data as $row)
  2. {
  3. $content1="<node>\n";
  4. $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);

  5.         $content1='<parts property="sn">';
  6.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  7.         $content1=$row['parts_sn'];
  8.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  9.         $content1="</parts>\n";
  10.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  11.         $content1='<parts property="oldno">';
  12.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  13.         $content1=$row['parts_old_No'];
  14.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  15.         $content1="</parts>\n";
  16.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  17.         $content1='<parts property="name">';
  18.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  19.         $content1=$row['parts_name'];
  20.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  21.         $content1="</parts>\n";
  22.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  23.         $content1='<parts property="type">';
  24.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  25.         $content1=$row['parts_type'];       
  26.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  27.         $content1="</parts>\n";
  28.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  29.         $content1='<parts property="note">';
  30.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  31.         $content1=$row['parts_notes_i'];
  32.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  33.         $content1="</parts>\n";
  34.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  35.         $content1='<parts property="no1">';
  36.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  37.         $content1=$row['parts_No_1'];
  38.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  39.         $content1="</parts>\n";
  40.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  41.         $content1='<parts property="no2">';
  42.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  43.         $content1=$row['parts_No_2'];
  44.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  45.         $content1="</parts>\n";
  46.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  47.         $content1='<parts property="no3">';
  48.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  49.         $content1=$row['parts_No_3'];
  50.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  51.         $content1="</parts>\n";
  52.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  53.         $content1='<parts property="unit">';
  54.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  55.         $content1=$row['parts_unit'];
  56.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  57.         $content1="</parts>\n";
  58.         $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  59. $content1="</node>\n";
  60. $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);
  61. }
  62. $content1="</data>";
  63. $content .=iconv(mb_detect_encoding($content1), "UTF-8",$content1);

复制代码

结果如下

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. - <data>
  3. - <node>
  4.   <parts property="sn">458</parts>
  5.   <parts property="oldno">843003</parts>
  6.   <parts property="name" />
  7.   <parts property="type" />
  8.   <parts property="note" />
  9.   <parts property="no1">8401013-**</parts>
  10.   <parts property="no2">1</parts>
  11.   <parts property="no3">3044</parts>
  12.   <parts property="unit">件</parts>
  13.   </node>
  14. - <node>
  15.   <parts property="sn">459</parts>
  16.   <parts property="oldno">843007</parts>
  17.   <parts property="name">发动机罩锁总成</parts>
  18.   <parts property="type" />
  19.   <parts property="note" />
  20.   <parts property="no1">8402100-**</parts>
  21.   <parts property="no2">1</parts>
  22.   <parts property="no3">3044</parts>
  23.   <parts property="unit" />
  24.   </node>
  25. - <node>
  26.   <parts property="sn">461</parts>
  27.   <parts property="oldno">842002</parts>
  28.   <parts property="name" />
  29.   <parts property="type">B=400</parts>
  30.   <parts property="note" />
  31.   <parts property="no1">8403012-**</parts>
  32.   <parts property="no2">1</parts>
  33.   <parts property="no3">3044</parts>
  34.   <parts property="unit">件</parts>
  35.   </node>

复制代码

[ 本帖最后由 LiveHappy 于 2006-10-11 10:13 编辑 ]

论坛徽章:
0
10 [报告]
发表于 2006-10-11 21:27 |只看该作者
过滤敏感字符。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP