1 回答
![?](http://img1.sycdn.imooc.com/533e4c0500010c7602000200-100-100.jpg)
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 关注
- 100 浏览
添加回答
举报