在將其轉換爲utf-8之前,您需要知道它是什麼字符集。 如果你無法弄清楚,你不能以任何理智的方式將其轉換爲utf8 ..然而,如果編碼無法確定,將其轉換爲utf-8的一種瘋狂方法是,將 簡單地剝離這不正好是UTF-8格式的任何有效字節,你 也許能夠使用它作爲後備...
警告,未經測試的代碼(有急事突然IM),但看起來像這樣的:
foreach ($datas as $data) {
$encoding = guess_encoding ($data);
if (empty ($encoding)) {
// encoding cannot be determined...
// as a fallback, we simply strip any bytes that isnt valid utf-8...
// obviously this isn't a reliable conversion scheme.
// also this could probably be improved
$data = iconv ("ASCII", "UTF-8//TRANSLIT//IGNORE", $text);
} else {
$data = mb_convert_encoding ($data, 'UTF-8', $encoding);
}
$row [] = explode (',', $data);
}
function guess_encoding(string $str): string {
$blacklist = array (
'pass',
'auto',
'wchar',
'byte2be',
'byte2le',
'byte4be',
'byte4le',
'BASE64',
'UUENCODE',
'HTML-ENTITIES',
'7bit',
'8bit'
);
$encodings = array_flip (mb_list_encodings());
foreach ($blacklist as $tmp) {
unset ($encodings [$tmp]);
}
$encodings = array_keys ($encodings);
$detected = mb_detect_encoding ($str, $encodings, true);
return (string) $detected;
}
可以試試的任何示例文件? –
你可能會在這裏找到你的答案:https://stackoverflow.com/a/7980354/1348344 –
所以最好的解決方案是檢測它是否包含utf-8? –