1 回答

TA貢獻1797條經驗 獲得超6個贊
不要使用 utf8 編碼。它僅適用于從 ISO-8859-1 轉換,不適用于 Windows-1252。
https://www.php.net/manual/en/function.utf8-encode.php
第二個問題是您的代碼進行了雙重編碼。我標記了兩個將字符串轉換為 UTF-8 的函數。
$temp = iconv(mb_detect_encoding($tmp, mb_detect_order(), true), "UTF-8", utf8_encode($tmp));
/* ^^^^^ ^^^^^^^^^^^ */
如果下面的代碼不起作用,我將調試mb_detect_encoding($tmp, mb_detect_order(), true). 的默認值mb_detect_order()可能遠不適合您的情況。
https://www.php.net/manual/en/function.mb-detect-encoding.php
https://www.php.net/manual/en/function.mb-detect-order.php
$temp = iconv(mb_detect_encoding($tmp, mb_detect_order(), true), "UTF-8", $tmp);
您可以使用mb_convert_encoding(). https://www.php.net/manual/en/function.mb-convert-encoding.phpiconv
對于您的問題,我將編寫以下代碼:
/* If there are no Asian languages, the UTF-8 is the only encoding the mb_detect_encoding can recognize. */
if (mb_detect_encoding($tmp, 'UTF-8')) {
$temp = $tmp;
} else {
/* It is not UTF-8. Assume WINDOWS-1252. */
$temp = mb_convert_encoding($tmp, 'UTF-8', 'WINDOWS-1252');
}
很難可靠地檢測到特定的單字節編碼。我不知道任何為此構建的 PHP 函數。
- 1 回答
- 0 關注
- 136 瀏覽
添加回答
舉報