关于 http://www.gnu.org/software/libiconv/中文编码大概有20种:
Chinese EUC-CN, HZ, GBK, CP936, GB18030, EUC-TW, BIG5, CP950, BIG5-HKSCS, BIG5-HKSCS:2004, BIG5-HKSCS:2001, BIG5-HKSCS:1999, ISO-2022-CN, ISO-2022-CN-EXT
所以我有一个不是 UTF-8 的文本文件。它是 ASCII。我想使用 iconv()
将其转换为 UTF-8。但为此我需要知道源的字符编码。
我不会中文怎么办? :(
我注意到:
$str = iconv('GB18030', 'UTF-8', $str);
file_put_contents('file.txt', $str);
生成一个 UTF-8 编码文件,而我试过的其他编码(CP950、GBK 和 EUC-CN)生成一个 ASCII 文件。这是否意味着 iconv
能够检测给定字符串的输入编码是否错误?
最佳答案
这可能满足您的需求(但我真的不知道)。设置语言环境和 utf8_decode,并使用 mb_check_encoding
而不是 mt_detect_encoding 似乎可以提供一些有用的输出。
// some text from http://chinesenotes.com/chinese_text_l10n.php
// have tried both as string and content loaded from a file
$chinese = '譧躆 礛簼繰 剆坲姏 潧 騔鯬 跠 瘱瘵瘲 忁曨曣 蛃袚觙';
$chinese=utf8_decode($chinese);
$chinese_encodings ='EUC-CN,HZ,GBK,CP936,GB18030,EUC-TW,BIG5,CP950,BIG5-HKSCS,BIG5-HKSCS:2004,BIG5-HKSCS:2001,BIG5-HKSCS:1999,ISO-2022-CN,ISO-2022-CN-EXT';
$encodings = explode(',',$chinese_encodings);
//set chinese locale
setlocale(LC_CTYPE, 'Chinese');
foreach($encodings as $encoding) {
if (@mb_check_encoding($chinese, $encoding)) {
echo 'The string seems to be compatible with '.$encoding.'<br>';
} else {
echo 'Not compatible with '.$encoding.'<br>';
}
}
输出
The string seems to be compatible with EUC-CN
The string seems to be compatible with HZ
The string seems to be compatible with GBK
The string seems to be compatible with CP936
Not compatible with GB18030
The string seems to be compatible with EUC-TW
The string seems to be compatible with BIG5
The string seems to be compatible with CP950
Not compatible with BIG5-HKSCS
Not compatible with BIG5-HKSCS:2004
Not compatible with BIG5-HKSCS:2001
Not compatible with BIG5-HKSCS:1999
Not compatible with ISO-2022-CN
Not compatible with ISO-2022-CN-EXT
完全是猜测。现在它至少似乎可以识别一些中文编码。如果完全是垃圾,请删除。
关于php - 如何检测哪种类型的中文编码有文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19059158/