首页 > 代码库 > 编码格式,网上看了资料还是乱码,就试下这个吧
编码格式,网上看了资料还是乱码,就试下这个吧
- /**
- * 将非GBK字符集的编码转为GBK
- *
- * @param mixed $mixed 源数据
- *
- * @return mixed GBK格式数据
- */
- function charsetToGBK($mixed)
- {
- if (is_array($mixed)) {
- foreach ($mixed as $k => $v) {
- if (is_array($v)) {
- $mixed[$k] = charsetToGBK($v);
- } else {
- $encode = mb_detect_encoding($v, array(‘ASCII‘, ‘UTF-8‘, ‘GB2312‘, ‘GBK‘, ‘BIG5‘));
- if ($encode == ‘UTF-8‘) {
- $mixed[$k] = iconv(‘UTF-8‘, ‘GBK‘, $v);
- }
- }
- }
- } else {
- $encode = mb_detect_encoding($mixed, array(‘ASCII‘, ‘UTF-8‘, ‘GB2312‘, ‘GBK‘, ‘BIG5‘));
- //var_dump($encode);
- if ($encode == ‘UTF-8‘) {
- $mixed = iconv(‘UTF-8‘, ‘GBK‘, $mixed);
- }
- }
- return $mixed;
- }
- /**
- * 将非UTF-8字符集的编码转为UTF-8
- *
- * @param mixed $mixed 源数据
- *
- * @return mixed utf-8格式数据
- */
- function charsetToUTF8($mixed)
- {
- if (is_array($mixed)) {
- foreach ($mixed as $k => $v) {
- if (is_array($v)) {
- $mixed[$k] = charsetToUTF8($v);
- } else {
- $encode = mb_detect_encoding($v, array(‘ASCII‘, ‘UTF-8‘, ‘GB2312‘, ‘GBK‘, ‘BIG5‘));
- if ($encode == ‘EUC-CN‘) {
- $mixed[$k] = iconv(‘GBK‘, ‘UTF-8‘, $v);
- }
- }
- }
- } else {
- $encode = mb_detect_encoding($mixed, array(‘ASCII‘, ‘UTF-8‘, ‘GB2312‘, ‘GBK‘, ‘BIG5‘));
- if ($encode == ‘EUC-CN‘) {
- $mixed = iconv(‘GBK‘, ‘UTF-8‘, $mixed);
- }
- }
- return $mixed;
- }
编码格式,网上看了资料还是乱码,就试下这个吧
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。