php中unserialize返回false的解決方案_php技巧

來源:互聯網
上載者:User

本文執行個體講述了php中unserialize返回false的解決方案,分享給大家供大家參考。具體方法如下:

php 提供serialize(序列化) 與unserialize(還原序列化)方法。
使用serialize序列化後,再使用unserialize還原序列化就可以擷取原來的資料。

先來看看如下程式執行個體:

<?php $arr = array(   'name' => 'fdipzone',   'gender' => 'male' );  $str = serialize($arr); //序列化 echo 'serialize str:'.$str."\r\n\r\n";  $content = unserialize($str); // 還原序列化 echo "unserialize str:\r\n"; var_dump($content); ?> 

輸出:

serialize str:a:2:{s:4:"name";s:8:"fdipzone";s:6:"gender";s:4:"male";}  unserialize str: array(2) {  ["name"]=>  string(8) "fdipzone"  ["gender"]=>  string(4) "male" } 

但下面這個例子還原序列化會返回false

<?php $str = 'a:9:{s:4:"time";i:1405306402;s:4:"name";s:6:"新晨";s:5:"url";s:1:"-";s:4:"word";s:1:"-";s:5:"rpage";s:29:"http://www.baidu.com/test.html";s:5:"cpage";s:1:"-";s:2:"ip";s:15:"117.151.180.150";s:7:"ip_city";s:31:"中國北京市 北京市移動";s:4:"miao";s:1:"5";}'; var_dump(unserialize($str)); // bool(false) ?> 

檢查序列化後的字串,發現出問題是在兩處地方:

s:5:"url"
s:29:"http://www.baidu.com/test.html"
這兩處應為
s:3:"url"
s:30:"http://www.baidu.com/test.html"

出現這種問題的原因是序列化資料時的編碼與還原序列化時的編碼不一致導致,例如資料庫是latin1和UTF-8字元長度不一樣。
另外有可能出問題的還有單雙引號,ascii字元"\0"被解析為 '\0',\0在C中是字串的結束符等於chr(0),錯誤解析後算了2個字元。
\r在計算長度時也會出問題。

解決方案如下:

// utf8 function mb_unserialize($serial_str) {   $serial_str= preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $serial_str );   $serial_str= str_replace("\r", "", $serial_str);   return unserialize($serial_str); }  // ascii function asc_unserialize($serial_str) {   $serial_str = preg_replace('!s:(\d+):"(.*?)";!se', '"s:".strlen("$2").":\"$2\";"', $serial_str );   $serial_str= str_replace("\r", "", $serial_str);   return unserialize($serial_str); } 

例子:

echo '<meta http-equiv="content-type" content="text/html; charset=utf-8">';  // utf8 function mb_unserialize($serial_str) {   $serial_str= preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $serial_str );   $serial_str= str_replace("\r", "", $serial_str);   return unserialize($serial_str); }  $str = 'a:9:{s:4:"time";i:1405306402;s:4:"name";s:6:"新晨";s:5:"url";s:1:"-";s:4:"word";s:1:"-";s:5:"rpage";s:29:"http://www.baidu.com/test.html";s:5:"cpage";s:1:"-";s:2:"ip";s:15:"117.151.180.150";s:7:"ip_city";s:31:"中國北京市 北京市移動";s:4:"miao";s:1:"5";}';  var_dump(unserialize($str));  // false  var_dump(mb_unserialize($str)); // 正確 

使用處理過單雙引號,過濾\r的mb_unserialize方法就能成功還原序列化了。

使用unserialize:

bool(false) 
 
使用mb_unserialize

array(9) {  ["time"]=>  int(1405306402)  ["name"]=>  string(6) "新晨"  ["url"]=>  string(1) "-"  ["word"]=>  string(1) "-"  ["rpage"]=>  string(30) "http://www.baidu.com/test.html"  ["cpage"]=>  string(1) "-"  ["ip"]=>  string(15) "117.151.180.150"  ["ip_city"]=>  string(31) "中國北京市 北京市移動"  ["miao"]=>  string(1) "5" }

希望本文所述對大家PHP程式設計的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.