iconv函数提示错误,解决!

  在网上看到json的文章,打算测试一下,结果,出现错误,先附上代码:

 

[php]  view plain  copy
  1. <?<a href="http://lib.csdn.net/base/php" class='replace_word' title="PHP知识库" target='_blank' style='color:#df3434; font-weight:bold;'>PHP</a>  
  2. @header("Content-type:text/html;charset=GB2312");  
  3. $arr = array ('name'=>"贾朝藤",'age'=>19,'sex'=>"男",'add'=>"中国石家庄");  
  4. echo json_encode($arr);  
  5. echo "<br/>";  
  6. $name = iconv("GBK","UTF-8","贾朝藤");  
  7. $sex = iconv("GBK","UTF-8","男");  
  8. $add = iconv("GBK","UTF-8","中国石家庄");  
  9. $arr = array ('name'=>"$name",'age'=>19,'sex'=>"$sex",'add'=>"$add");  
  10. echo $str=json_encode($arr);  
  11. ?>  

  错误信息如下:

 

[xhtml]  view plain  copy
  1. {"name":"/u8d3e/u671d/u85e4","age":19,"sex":"/u7537","add":"/u4e2d/u56fd/u77f3/u5bb6/u5e84"} Notice: iconv() [function.iconv]: Detected an illegal character in input string in D:/wamp/www/ajax/js_php.<a href="http://lib.csdn.net/base/php" class='replace_word' title="PHP知识库" target='_blank' style='color:#df3434; font-weight:bold;'>php</a> on line 6 Notice: iconv() [function.iconv]: Detected an illegal character in input string in D:/wamp/www/ajax/js_php.php on line 7 Notice: iconv() [function.iconv]: Detected an illegal character in input string in D:/wamp/www/ajax/js_php.php on line 8 {"name":"/u7490/u70ac","age":19,"sex":"","add":"/u6d93"}   

  之后百度,谷歌了一番,得到两个方法:
  1.把gb2312换为gbk ,同样出错,错误信息如下

 

[xhtml]  view plain  copy
  1. "name":"/u8d3e/u671d/u85e4","age":19,"sex":"/u7537","add":"/u4e2d/u56fd/u77f3/u5bb6/u5e84"}  
  2. Notice: iconv() [function.iconv]: Detected an incomplete multibyte character in input string in D:/wamp/www/ajax/js_php.php on line 6  
  3. Notice: iconv() [function.iconv]: Detected an incomplete multibyte character in input string in D:/wamp/www/ajax/js_php.php on line 7  
  4. Notice: iconv() [function.iconv]: Detected an illegal character in input string in D:/wamp/www/ajax/js_php.php on line 8  
  5. {"name":"/u7490/u70ac/u6e5e/u9498","age":19,"sex":"/u9422","add":"/u6d93"}  

  2.在UTF-8后加//IGNORE ,加上后提示如下:

 

[xhtml]  view plain  copy
  1. {"name":"/u8d3e/u671d/u85e4","age":19,"sex":"/u7537","add":"/u4e2d/u56fd/u77f3/u5bb6/u5e84"}  
  2. Notice: iconv() [function.iconv]: Detected an incomplete multibyte character in input string in D:/wamp/www/ajax/js_php.php on line 6  
  3. Notice: iconv() [function.iconv]: Detected an incomplete multibyte character in input string in D:/wamp/www/ajax/js_php.php on line 7  
  4. {"name":"/u7490/u70ac/u6e5e/u9498","age":19,"sex":"/u9422","add":"/u6d93/u9365/u754c/u7176/u7039/u8dfa/u7c1e"}  

  不解,之后在论坛提问,得到问题所在:

  我的php文件本身是utf-8编码的,把它转为ansi这个问题就不存在

  修改之后,确实,显示正常了!

  论坛还有个朋友,给我推荐了个函数

  string mb_convert_encoding ( string $str , string $to_encoding [, mixed $from_encoding ] )

  需要编译时加一个参数--enable-mbstring

  对了,附上json转换php数组跟js数组的文章地址也加上,个人感觉文章不错

  http://www.phpchina.com/index.php?action-viewthread-tid-15585


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值