不能反序列化字符串

时间:2018-12-16 17:15:42

标签: php serialization

当我尝试对以下字符串进行反序列化时:

a:3:{i:0;s:19:\"Sales+%2F+Customers\";i:1;s:78:\"Micro+Business+%28less+than+10+employees+%26+turnover+under+%C2%A32+million%29\";i:2;s:13:\"Manufacturing\";}

返回false,我得到以下警告:

WARNING: Error at offset 9 of 158 bytes

它是通过在数组上调用serialize生成的,尽管对我来说看起来是有效的,但我不明白问题是什么?

1 个答案:

答案 0 :(得分:0)

在这种情况下,斜杠无效,请使用stripslashes()。下面的演示代码:

<pre>
<?php
$x = "a:3{i:0;s:19:\"Sales+%2F+Customers\";i:1;s:78:\"Micro+Business+%28less+than+10+employees+%26+turnover+under+%C2%A32+million%29\";i:2;s:13:\"Manufacturing\";}";

echo "unserializing $x: " . PHP_EOL;

try {
    print_r(unserialize($x));
    echo PHP_EOL;
} catch (\Exception $e) {
    echo $e->getMessage() . PHP_EOL;
}

$y = "a:3{i:0;s:19:\\\"Sales+%2F+Customers\\\";i:1;s:78:\\\"Micro+Business+%28less+than+10+employees+%26+turnover+under+%C2%A32+million%29\\\";i:2;s:13:\\\"Manufacturing\\\";}";

echo "unserializing $y: " . PHP_EOL;

try{
    print_r(unserialize($y));
    echo PHP_EOL;
} catch (\Exception $e) {
    echo $e->getMessage() . PHP_EOL;
}

echo "unserializing stripslashes($y): " . PHP_EOL;

try{
    print_r(unserialize(stripslashes($y)));
} catch (\Exception $e) {
    echo $e->getMessage() . PHP_EOL;
}
?>
</pre>