class Phpclass {
function __construct() {
}
/**
* 转换字符编码
* @param string $fContents 需要转换编码格式的内容
* @param string $from 原编码格式
* @param string $to 新编码格式
* @return string
*/
public 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);
} elseif (function_exists('iconv')) {
return iconv($from, $to, $fContents);
} else {
return $fContents;
}
}else {
return $fContents;
}
}//auto_charset
}