php - 如何检测哪种类型的中文编码有文本文件?

标签 php string text utf-8 iconv

关于 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/

相关文章:

php - 如何从没有匹配列的表中获取数据?

swift - 使用 firstIndex(of : Character) in Swift? 时是否总是需要 Nil-Coalescing

java - 如何将对象数组合并为单个格式化字符串

javascript - 在 JQuery 中执行 JavaScript .replace

javascript - jQuery 选择器混淆

parsing - 从文本文件lua中解析科学数字

php - 如何在杀死 php 脚本后杀死 passthru 进程?

php - 使用 php cron 脚本花费时间进行电子商务门户的数据库更新

mongodb - mongodb 使用 mongoose 进行文本搜索

php - Laravel-5 和 Multi-Tenancy 数据库设置