编码格式,网上看了资料还是乱码,就试下这个吧
Posted nike_ha
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编码格式,网上看了资料还是乱码,就试下这个吧相关的知识,希望对你有一定的参考价值。
- /**
- * 将非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;
- }
以上是关于编码格式,网上看了资料还是乱码,就试下这个吧的主要内容,如果未能解决你的问题,请参考以下文章
解决SSM框架中,存储到mysql数据库中乱码问题的究极方案
我同时安装了linux和Windows操作系统,但是共享文件的时候总是出现乱码问题