PHP Error: unserialize() [function.unserialize]: Error at offset 118 of 512
Lately I got this error in my script while trying to decode serialized UTF-8 string:
unserialize() [function.unserialize]: Error at offset 118 of 512
Here’s a simple solution I found. Instead of using unserialize() you should use mb_unserialize():
function mb_unserialize($str)
{
$res = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $str );
return unserialize($res);
}
You can also think about replacing in your program serialize() + unserialize() with json_encode() + json_decode(). Your data should be safer then.
September 27th, 2011 at 3:11 am
Your can use base64 or url to encode/encode your content before serialize()
February 4th, 2012 at 10:52 am
Hey!
Thanks a lot. That solved my “offset” problem…
February 6th, 2012 at 9:07 pm
Yes, but base64 makes the data longer (takes more space) than json_encode.