php 将 latin1 转换为 UTF8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5232344/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:42:03  来源:igfitidea点击:

Convert latin1 to UTF8

phpmysql

提问by WEBProject

I have a DB - with the table articles.

我有一个数据库 - 与表文章。

I want to convert the title, and content field to utf8 now - all data looks like this: פ×?ר×?×? ר×¢×? ×?פ×a×— ר×?×?×?×a!I want it to become normal hebrew characters.

我想现在将标题和内容字段转换为 utf8 - 所有数据看起来像这样:פ×?ר×?×? ר×¢×? ×?פ×a×— ר×?×?×?×a!我希望它成为正常的希伯来语字符。

Thanks

谢谢

采纳答案by Vladislav Rastrusny

You can make mysqldump from this database. Then download something like Notepad++, open dump file, convert it to UTF8, then replace through the file all encodings to utf-8 including the first SET NAMES operator.

您可以从此数据库制作 mysqldump。然后下载 Notepad++ 之类的东西,打开转储文件,将其转换为 UTF8,然后将文件中的所有编码替换为 utf-8,包括第一个 SET NAMES 运算符。

If you make dump to file via phpMyAdmin (with default settings) use output file encoding ISO-8859-1 instead of UTF-8 as you can see by default.

如果您通过 phpMyAdmin(使用默认设置)转储到文件,请使用输出文件编码 ISO-8859-1 而不是 UTF-8,正如您在默认情况下看到的那样。

回答by tawfekov

if you need to convert the whole database , you can back it as databaseback.sqlfile then form your command line iconv -f latain -t utf-8 < databaseback.sql > databaseback.utf8.sql

如果您需要转换整个数据库,您可以将其作为databaseback.sql文件备份,然后形成您的命令行 iconv -f latain -t utf-8 < databaseback.sql > databaseback.utf8.sql

you can use the http://www.php.net/manual/en/function.iconv.php
to convert each row in php in case you don't have command line access

如果您没有命令行访问权限,您可以使用http://www.php.net/manual/en/function.iconv.php
转换 php 中的每一行

and lastly don't forget to convert the collation of each field in phpmyadmin , then you can resotre the utf8 back easily

最后不要忘记在 phpmyadmin 中转换每个字段的排序规则,然后您可以轻松地重新获取 utf8

update

更新

if you got iconv is not recognized, it means that you don't have iconvinstalled

如果你有iconv is not recognized,这意味着你没有iconv安装

much more easier solution is :Migrating MySQL Data to Unicode

更简单的解决方案是:将MySQL 数据迁移到 Unicode

http://daveyshafik.com/archives/166-migrating-mysql-data-to-unicode.html

http://daveyshafik.com/archives/166-migrating-mysql-data-to-unicode.html

回答by Evandro

The following MySQL function will return the correct utf8 string after double-encoding:

以下 MySQL 函数将在双重编码后返回正确的 utf8 字符串:

CONVERT(CAST(CONVERT(field USING latin1) AS BINARY) USING utf8)

It can be used with an UPDATE statement to correct the fields:

它可以与 UPDATE 语句一起使用以更正字段:

UPDATE tablename SET field = CONVERT(CAST(CONVERT(field USING latin1) AS BINARY) USING utf8);

回答by Tobias

You can write a little php script which does the conversion. See http://www.php.net/manual/en/function.mb-detect-encoding.phpand http://php.net/manual/en/function.mb-convert-encoding.phpThis is how I did this.

您可以编写一个小的 php 脚本来进行转换。参见http://www.php.net/manual/en/function.mb-detect-encoding.phphttp://php.net/manual/en/function.mb-convert-encoding.php这就是我的方式做过这个。

And remember to use strict mode! http://www.php.net/manual/en/function.mb-detect-encoding.php#102510

并记住使用严格模式!http://www.php.net/manual/en/function.mb-detect-encoding.php#102510

In pseudocode it would be sth. like this:

在伪代码中,它将是…… 像这样:

str = getDataAsString()
if(!isUTF8(str)) {
  str = convert2UTF8(str)
}
saveStr2DB()

回答by diEcho

try

尝试

ALTER TABLE `tablename` CHANGE `field_name` `field_name` VARCHAR( 200 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL