开发者

Is PHP serialize function compatible UTF-8?

I have a site I want to migrate from ISO to UTF-8.

I have a record in database indexed by the following primary key :

s:22:"Informations générales";

The problem is, now (with UTF-8), when I serialize the string, I get :

s:24:"Informations générales";

(notice the size of the string is now the numbe开发者_开发知识库r of bytes, not string length)

So this is not compatible with non-utf8 previous records !

Did I do something wrong ? How could I fix this ?

Thanks


The behaviour is completely correct. Two strings with different encodings will generate different byte streams, thus different serialization strings.


Dump the database in latin1.

In the command line:

sed  -e 's/latin1/utf8/g' -i ./DBNAME.sql

Import the file converted to a new database in UTF-8.

Use a php script to update each field. Make a query, loop through each field and update the serialized string using this:

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

After that, I was able to use unserialize() and everything working with UTF-8.


To unserialize an utf-8 encoded serialized array:

$array = @unserialize($arrayFromDatabase);
if ($array === false) {
  $array =  @unserialize(utf8_decode($arrayFromDatabase)); //decode first
  $array = array_map('utf8_encode', $array ); // encode the array again
}


PHP 4 and 5 do not have built-in Unicode support; I believe PHP 6 is starting to add more Unicode support although I'm not sure how complete that is.


You did nothing wrong. PHP prior to v6 just isn't Unicode aware, and as such doesn't support it, if you don't beat it to be (i.e., via the mbstring extension or other means).

We here wrote our own wrapper around serialize() to remedy this. You could, too, move to other serialization techniques, like JSON (with json_encode() and json_decode() in PHP since 5.2.0).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜