Main menu

PHP serialization and line ending problem

Published by dnovikov on Tue, 12/17/2013 - 14:04

When you serialize data containing text strings which have mixed line endings ("\n" and "\r\n"), unserialization often does not work. This bug is described at

https://bugs.php.net/bug.php?id=45573

To fix the issue use json_encode around serialized string:

$data_serialized = json_encode(serialize($data));

JSON format expects explicit line ending characters displayed as user readable text in form of "\n" and "\r\n".

After that decode string back and unserialize it:

$data_unserialized = unserialize(json_encode($data));

 

Comments

Please update the code for the unserialize like this:
$data_unserialized = unserialize(json_decode($data));

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.