2016-03-04 19 views
1

下面是代码:2个相同的字符串,一个在反序列化时给出错误,另一个没有。为什么?

<?php 
$txt = 'a:1:{s:14:"exclude_stores";a:3:{i:0;s:7:"phoenix";i:1;s:8:"chandler";i:2;s:6:"tucson";}}' //serialized PHP array 

$str = htmlspecialchars(urldecode($txt)); 
$str = preg_replace('/\&lt;\?.*\?\&gt;/ims', '', $str); //get rid of php tags/code enclosed in <? ... > 

$str = preg_replace('/\&gt;/ims', '>', $str); //change &gt; back to > 
?> 

在这一点上,如果我这样做:

<?php 
echo gettype($txt) . ' ' . $txt . '<br>'; 
echo gettype($str) . ' ' . $str; 
?> 

我得到:

string a:1:{s:14:"exclude_stores";a:3:{i:0;s:7:"phoenix";i:1;s:8:"chandler";i:2;s:6:"tucson";}} 

string a:1:{s:14:"exclude_stores";a:3:{i:0;s:7:"phoenix";i:1;s:8:"chandler";i:2;s:6:"tucson";}} 

琴弦似乎是完全一样的。然后,如果我这样做:

<?php 
$u1 = unserialize($txt); 
print_r($u1); 
$u2 = unserialize($str); 
print_r($u2); 
?> 

我得到: 阵列([exclude_stores] =>数组([0] =>凤[1] =>钱德勒[2] =>图森))

Notice: unserialize(): Error at offset 5 of 128 bytes in ... 

有些事情正在发生$ str,反序列化不喜欢,但我无法弄清楚它是什么。屏幕上的字符串看起来完全一样,当我将它们粘贴到N ++中时,它们看起来完全一样。

我怀疑htmlspecialchars,urldecode或preg_replace正在对我看不到的文本做些什么。任何帮助表示赞赏。

回答

2

字符串是不同的,但是他们出现在同一个屏幕上,因为用htmlspecialchars被转换为&quot;这使得您的浏览器屏幕上的“符号。

尝试从CLI运行脚本来看看我的意思

$ str是等于:

string a:1:{s:14:&quot;exclude_stores&quot;;a:3:{i:0;s:7:&quot;phoenix&quot;;i:1;s:8:&quot;chandler&quot;;i:2;s:6:&quot;tucson&quot;;}} 

你可以看到为什么它不会反序列化:)

+0

问题解决了谢谢! – raphael75

相关问题