php实现转码的方式(支持数组类型转码)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php实现转码的方式(支持数组类型转码)相关的知识,希望对你有一定的参考价值。

由于将Array转换为json的json_encode()方法仅能将utf-8字符集进行转换,不是utf-8格式的中文字符会变成null,需要对Array中的字符进行统一转码,但是iconv()方法只能转换字符串类型编码,网上找到如下方法可以统一解决string类型和Array类型的转码问题

function auto_charset($fContents, $from=‘gbk‘, $to=‘utf-8‘) {
$from = strtoupper($from) == ‘UTF8‘ ? ‘utf-8‘ : $from;
$to = strtoupper($to) == ‘UTF8‘ ? ‘utf-8‘ : $to;
if (strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents))) {
return $fContents;
}
if (is_string($fContents)) {
if (function_exists(‘mb_convert_encoding‘)) {
return mb_convert_encoding($fContents, $to, $from);
} else if (function_exists(‘iconv‘)) {
return iconv($from, $to, $fContents);
} else {
return $fContents;
}
} else if (is_array($fContents)) {
foreach ($fContents as $key => $val) {
$_key = auto_charset($key, $from, $to);
$fContents[$_key] = auto_charset($val, $from, $to);
if ($key != $_key)
unset($fContents[$key]);
}
return $fContents;
}
else {
return $fContents;
}
}

以上是关于php实现转码的方式(支持数组类型转码)的主要内容,如果未能解决你的问题,请参考以下文章

FFMPEG实现的转码程序

JAVA实现获取本地图片然后转码的问题

对url中的参数进行转码

js中文转码url问题

115怎么转码

在iOS中进行utf8转码的时候怎么把等号也转码