将拉丁MySQL数据转换为utf8 [英] Converting latin mysql data to utf8

查看:138
本文介绍了将拉丁MySQL数据转换为utf8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在想使用utf 8,但是我所有的数据都是latin1,这是转换数据的有效方法是什么.我也知道如何将数据库的结构(字符集)更改为utf8,我想做的就是更改现有数据的字符集.

I want to use utf 8 right now , but all my data is latin1 , what is the efficient way to convert data . Also I know how to change database's structure(charset) to utf8 , What I want to do is changing charset of existing data .

这是我以前的设置,

  1. HTML输出:utf8
  2. HTML输入:utf8
  3. Php-mysql连接:latin1
  4. mysql(字段和表):latin1

这是我的新设置,希望这是创建多语言网站的最佳方法

Here are my new settings , and I hope this is the best way to create multi-language website

  1. HTML输出:utf8
  2. HTML输入:utf8
  3. Php-mysql连接:utf8
  4. sql(字段和表):utf8

推荐答案

如果将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

这篇关于将拉丁MySQL数据转换为utf8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆