使用 PHP 将 latin1_swedish_ci 转换为 utf8
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6646949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting latin1_swedish_ci to utf8 with PHP
提问by Bibhas Debnath
I have a database filled with values like a?¥a¢a—a?¥ Dhaka a?¥a¢a—a?¥
(Which should be ??●? Dhaka ??●?
) as I didnt specify the collation while creating the database.
Now I want to Fix it. I cannot fetch the data again from where I got it from at the first place. So I was thinking if it might be possible to fetch the data in a php script and convert it to the correct characters.
I've changed the collation of the database and the fields to utf8_general_ci
..
我有一个数据库,a?¥a¢a—a?¥ Dhaka a?¥a¢a—a?¥
其中填充了(应该是??●? Dhaka ??●?
)之类的值,因为我在创建数据库时没有指定排序规则。
现在我想修复它。我无法从一开始的地方再次获取数据。所以我在想是否有可能在 php 脚本中获取数据并将其转换为正确的字符。
我已将数据库和字段的排序规则更改为utf8_general_ci
..
回答by Emil Vikstr?m
The collation is NOT the same as the character set. The collation is only used for sorting and comparison of text (that's why there's a language term in there). The actual character set may be different.
排序规则与字符集不同。排序规则仅用于文本的排序和比较(这就是为什么那里有一个语言术语)。实际的字符集可能会有所不同。
The most common failure is not in the database but rather in the connection between PHP and MySQL. The default charset for the connection is usually ISO-8859-1. You need to change that the first thing you do after connecting, using either the SQL query SET NAMES 'utf-8';
or the mysql_set_charsetfunction.
最常见的故障不在数据库中,而是在 PHP 和 MySQL 之间的连接中。连接的默认字符集通常是 ISO-8859-1。您需要使用 SQL 查询SET NAMES 'utf-8';
或mysql_set_charset函数更改连接后所做的第一件事。
Also check the character set of your tables. This may be wrong as well if you have not specified UTF-8 to begin with (again: this is not the same as the collation). But make sure to take a backup before changing anything here. MySQL will try to convert the charset from the previous one, so you may need to reload the data from backup if you have actually saved UTF-8 data in ISO-8859-1 tables.
还要检查表的字符集。如果您没有指定 UTF-8 开始,这也可能是错误的(同样:这与排序规则不同)。但请确保在更改此处的任何内容之前进行备份。MySQL 将尝试从前一个字符集转换字符集,因此如果您实际上已将 UTF-8 数据保存在 ISO-8859-1 表中,则可能需要从备份中重新加载数据。
回答by AlienWebguy
I would look into mb_detect_encoding()and mb_convert_encoding()and see if they can help you.
我会研究mb_detect_encoding()和mb_convert_encoding(),看看它们是否可以帮助你。