如果将 utf8_encode() 应用于已经是 UTF8 的字符串,它将返回乱码的 UTF8 输出。
我做了一个函数来解决所有这些问题。它称为forceUTF8()。
您不需要知道字符串的编码是什么。它可以是 Latin1 (iso 8859-1) 或 UTF8,或者字符串可以是两者的混合。 forceUTF8() 会将所有内容转换为 UTF8。
我这样做是因为一项服务向我提供的数据完全混乱,将 UTF8 和 Latin1 混合在同一个字符串中。
Usage:
$utf8_string = forceUTF8($utf8_or_latin1_or_mixed_string);
$latin1_string = forceLatin1($utf8_or_latin1_or_mixed_string);
我添加了另一个函数,fix UTF 8(),它将修复每个看起来乱码的 UTF8 字符串。
Usage:
$utf8_string = fixUTF8($garbled_utf8_string);
例子:
echo fixUTF8("Fédération Camerounaise de Football");
echo fixUTF8("Fédération Camerounaise de Football");
echo fixUTF8("FÃÂédÃÂération Camerounaise de Football");
echo fixUTF8("Fédération Camerounaise de Football");
将输出:
Fédération Camerounaise de Football
Fédération Camerounaise de Football
Fédération Camerounaise de Football
Fédération Camerounaise de Football
更新:我将它们转换为静态类,它们现在位于 Github 中:
https://github.com/neitanod/forceutf8 https://github.com/neitanod/forceutf8