如果您申请utf8_encode()
对于已经是 UTF8 的字符串,它将返回乱码的 UTF8 输出。
我做了一个函数来解决所有这些问题。叫做Encoding::toUTF8()
.
您不需要知道字符串的编码是什么。它可以是 Latin1 (ISO8859-1)、Windows-1252 或 UTF8,或者字符串可以是它们的混合。Encoding::toUTF8()
会将所有内容转换为 UTF8。
我这样做是因为一项服务给了我一个混乱的数据源,将这些编码混合在同一个字符串中。
Usage:
require_once('Encoding.php');
use \ForceUTF8\Encoding; // It's namespaced now.
$utf8_string = Encoding::toUTF8($mixed_string);
$latin1_string = Encoding::toLatin1($mixed_string);
我添加了另一个函数 Encoding::fixUTF8(),它将修复每个因多次编码为 UTF8 而看起来乱码的 UTF8 字符串。
Usage:
require_once('Encoding.php');
use \ForceUTF8\Encoding; // It's namespaced now.
$utf8_string = Encoding::fixUTF8($garbled_utf8_string);
例子:
echo Encoding::fixUTF8("Fédération Camerounaise de Football");
echo Encoding::fixUTF8("Fédération Camerounaise de Football");
echo Encoding::fixUTF8("FÃÂédÃÂération Camerounaise de Football");
echo Encoding::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
下载:
https://github.com/neitanod/forceutf8 https://github.com/neitanod/forceutf8