简体   繁体   中英

Convert latin1 to UTF8

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: פורטל רעל × ×¤×ª×— רשמית! I want it to become normal hebrew characters.

Thanks

if you need to convert the whole database , you can back it as databaseback.sql file then form your command line 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

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

update

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

much more easier solution is : Migrating MySQL Data to Unicode

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

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

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

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

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

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.

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.

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

And remember to use strict mode! 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()

尝试

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM