php - unserialize() Error at offset - Caused by Single Quote -
i getting error when trying unserialize data. following error occurs:
unserialize(): error @ offset 46 of 151 bytes
here serialized data:
s:151:"a:1:{i:0;a:4:{s:4:"name";s:15:"chloe o'gorman";s:6:"gender";s:6:"female";s:3:"age";s:3:"3_6";s:7:"present";s:34:"something frozen or jigsaw ";}}";
the error being caused single quote in data. how can alleviate problem when site , database working live? unfortunately cannot rewrite code responsible serializing , inserting data database. highly there multiple occurrences of problem across database.
is there function can use escape data?
after doing further research have found work around solution. according this blog post:
"it turns out if there's ", ', :, or ; in of array values serialization gets corrupted."
if working on site hadn't yet been put live, prevention method have been base64_encode
serialized data before stored in database so:
base64_encode( serialize( $my_data ) );
and then:
unserialize( base64_decode( $encoded_serialized_string ) );
when retrieving data.
however, cannot change has been stored in database, this helpful post provides solution works around problem:
$fixed_serialized_data = preg_replace_callback ( '!s:(\d+):"(.*?)";!', function($match) { return ($match[1] == strlen($match[2])) ? $match[0] : 's:' . strlen($match[2]) . ':"' . $match[2] . '";'; }, $my_data ); $result = unserialize( $fixed_serialized_data );
Comments
Post a Comment