phi*_*reo 6 php unicode cjk language-detection
给定一个已知为中文并以UTF-8编码的文本块,有没有办法确定它是简化还是传统?
我不知道这是否有效,但我会尝试使用 iconv 来查看它是否能在字符集之间正确转换,并将相同转换的结果与 //TRANSLIT 和 //IGNORE 进行比较。如果两个结果匹配,则字符集转换没有遇到任何无法翻译的字符,因此应该有一个匹配。
$test1 = iconv("UTF-8", "big5//TRANSLIT", $text);
$test2 = iconv("UTF-8", "big5//IGNORE", $text);
if ($test1 == $test2) {
echo 'traditional';
} else {
$test3 = iconv("UTF-8", "gb2312//TRANSLIT", $text);
$test4 = iconv("UTF-8", "gb2312//IGNORE", $text);
if ($test3 == $test4) {
echo 'simplified';
} else {
echo 'Failed to match either traditional or simplified';
}
}
Run Code Online (Sandbox Code Playgroud)