且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将拉丁MySQL数据转换为utf8

更新时间:2023-09-11 21:40:16

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

If you apply utf8_encode() to an already UTF8 string it will return a garbled UTF8 output.

我做了一个解决所有这些问题的功能.它称为forceUTF8().

I made a function that addresses all this issues. It´s called forceUTF8().

您不需要知道字符串的编码是什么.它可以是Latin1(iso 8859-1)或UTF8,或者字符串可以混合使用两者. forceUTF8()会将所有内容转换为UTF8.

You dont need to know what the encoding of your strings is. It can be Latin1 (iso 8859-1) or UTF8, or the string can have a mix of the two. forceUTF8() will convert everything to UTF8.

之所以这样做,是因为某项服务使我的数据馈送全乱了,在同一字符串中混合了UTF8和Latin1.

I did it because a service was giving me a feed of data all messed up, mixing UTF8 and Latin1 in the same string.

用法:

$utf8_string = forceUTF8($utf8_or_latin1_or_mixed_string);

$latin1_string = forceLatin1($utf8_or_latin1_or_mixed_string);

我包括了另一个函数fixUFT8(),它将修复每个看起来乱码的UTF8字符串.

I've included another function, fixUFT8(), wich will fix every UTF8 string that looks garbled.

用法:

$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

更新:我将eseese转换为静态类,并且它们现在位于Github中:

Update: I converted theese into a static class, and they live in Github now:

https://github.com/neitanod/forceutf8