下面是我对一个类似问题给出的另一个答案的抄本:

如果将utf8_encode()应用于已经存在的utf8字符串,它将返回一个乱码的utf8输出。

我做了一个处理所有这些问题的函数。它被称为encoding::toutf8()。

您不需要知道字符串的编码是什么。它可以是拉丁语1(iso 8859-1)、windows-1252或utf8,或者字符串可以混合使用。编码::to utf8()将所有内容转换为utf8。

我这么做是因为一个服务向我提供了一个数据源,所有的数据都乱七八糟的,在同一个字符串中混合了utf8和latin1。

用途:

$utf8_string = Encoding::toUTF8($utf8_or_latin1_or_mixed_string);

$latin1_string = Encoding::toLatin1($utf8_or_latin1_or_mixed_string);

下载:

更新:

我还包含了另一个函数encoding::fixuft8(),wich将修复每个看起来乱七八糟的utf8字符串。

用法:

$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

更新:我已经将函数(forceutf8)转换为一个名为encoding的类上的一系列静态函数。新函数是encoding::toutf8()。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐