Search code examples
phpmysqlserializationutf-8latin1

PHP function unserialize stop working after charset change (from latin1 to UTF-8)


I use the PHP function serialize to serialize an object with a big string, in the string are a special character "—". That object was save when the DB was using latin1 charset now I migrate the db to UTF-8.

I use the PHP function unserialize to get the object back, since I changed the charset to UTF-8 that function stop working. I don't know why.

I modify httpd.conf to use:

AddCharset UTF-8 .utf8
AddDefaultCharset UTF-8

php.ini:

default_charset = "UTF-8"

And CONVERT all MySQL data to UTF-8.

UPDATE I catch the php error when I call the unserialize function:

unserialize(): Error at offset 19146 of 23672 bytes in /xxx/xxx.php:18

Solution

  • I found that the length of the serialized string was wrong after change from latin1 to UTF-8. I fix the problem using this PHP:

    $content = preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", $content);
    

    I'm going to update the data base with the new string.