Home >Backend Development >PHP Tutorial > PHP的unserialize回来false

PHP的unserialize回来false

WBOY
WBOYOriginal
2016-06-13 13:06:321143browse

PHP的unserialize返回false

问题描述:如下是我序列化数组之后的数据,但是取出来之后,反序列化不了。
$a = 'a:2:{i:1;a:5:{s:8:"pic_sort";i:2;s:9:"pic_check";b:1;s:10:"pic_select";i:56;s:8:"pic_path";s:1:"ergregerg";s:8:"pic_link";s:2:"wesaddasd";}i:2;a:5:{s:8:"pic_sort";i:2;s:9:"pic_check";b:0;s:10:"pic_select";i:57;s:8:"pic_path";s:10:"4444";s:8:"pic_link";s:14:"http://fafa.com";}}';
print_r(unserialize($a));

问题原因:应该是从数据库中取出的编码和页面的编码不一致造成的。

解决方案:自定义如下的函数调用就可以了
???print_r(mb_unserialize($a));
???function mb_unserialize($serial_str) {?
?????$out = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $serial_str );?
?????return unserialize($out);?
???}?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn