// 使用默认字符编码识别可能导致识别错误
//$encoding = mb_detect_encoding($string, mb_detect_order(), true);
// 默认mb_detect_order() => ASCII,UTF-8
// 手动指定识别顺序
$encoding = mb_detect_encoding($string, 'GB2312,GBK,ASCII,UTF-8,ISO-8859-15', true);
if ($encoding) {
$string = mb_convert_encoding($string, "UTF-8", $encoding);
} else if (mb_detect_encoding($string, 'GB2312', true)) {
$string = mb_convert_encoding($string, "UTF-8", 'GB2312');
} else {
$string = mb_convert_encoding($string, "UTF-8", 'GBK');
}
4 评论