2016-03-04 21 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

相關問題