class_Chareset.php
2024-07-05 19:58 更新
<?php
/*
* @copyright Leyun internet Technology(Shanghai)Co.,Ltd
* @license http://www.dzzoffice.com/licenses/license.txt
* @package DzzOffice
* @link http://www.dzzoffice.com
* @author qchlian(3580164@qq.com)
*/
class Chareset{
// 自动转换字符集 支持数组转换
public function autocharset($string, $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($string) || (is_scalar($string) && !is_string($string))) {
//如果编码相同或者非字符串标量则不转换
return $string;
}
if (is_string($string)) {
if (function_exists('mb_convert_encoding')) {
return mb_convert_encoding($string, $to, $from);
} elseif (function_exists('iconv')) {
return iconv($from, $to, $string);
} else {
return $string;
}
} elseif (is_array($string)) {
foreach ($string as $key => $val) {
$_key = self::autocharset($key, $from, $to);
$string[$_key] = self::autocharset($val, $from, $to);
if ($key != $_key)
unset($string[$key]);
}
return $string;
}
else {
return $string;
}
}
}
?>
以上内容是否对您有帮助:
更多建议: