How to correct double-encoded UTF-8 strings sitting in MySQL utf8_general_ci fields?

后端 未结 6 1555
感动是毒
感动是毒 2020-12-15 07:42

I have to redesign a class where (amongst other things) UTF-8 strings are double-encoded wrongly:

$string = iconv(\'ISO-8859-1\', \'UTF-8\', $string);
:
$str         


        
6条回答
  •  清歌不尽
    2020-12-15 08:43

    Alter the table to change the column character set to Latin-1. You will now have singly-encoded UTF-8 strings, but sitting in a field whose collation is supposed to be Latin-1.

    What you do then is, change the column character set back to UTF-8 via the binary character set - that way MySQL doesn't convert the characters at any point.

    ALTER TABLE MyTable MODIFY MyColumn ... CHARACTER SET latin1
    ALTER TABLE MyTable MODIFY MyColumn ... CHARACTER SET binary
    ALTER TABLE MyTable MODIFY MyColumn ... CHARACTER SET utf8
    

    (is the correct syntax iirc; put the appropriate column type in where ... is)

提交回复
热议问题