Skip to content

Instantly share code, notes, and snippets.

@erasin
Last active December 21, 2015 19:08
Show Gist options
  • Save erasin/6351747 to your computer and use it in GitHub Desktop.
Save erasin/6351747 to your computer and use it in GitHub Desktop.
一些表单验证需要返回json数据,php的json_encode函数只支持utf-8编码,无奈只得iconv了,需要达到的效果是GBK数组转换成utf-8数组传给 son_encode函数。
<?php
//func 1. 将数组序列化后用iconv函数转换编码,之后再反序列化
unserialize(iconv('gbk','utf-8',serialize($array))); // 貌似经常报错
//func 2. 用构建数组原型的序列化方法,借助var_export函数,最终函数如下:
function array_iconv($in_charset,$out_charset,$arr){
return eval('return '.iconv($in_charset,$out_charset,var_export($arr,true).';'));
}
// test
var_dump(array_iconv("EUC-CN",'utf-8//IGNORE',$arr));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment