php unserialize函數反序列話數組

來源:互聯網
上載者:User

標籤:使用   io   資料   問題   cti   php   ef   return   

有沒有,遇到這種情況:使用serialize函數序列化一個數組後,然後想通過unserialize函數反序話,得到該數組,可是確報錯,得不到想要的結果。

從網上搜尋,發現很多網友都遇到相同的問題,好像說這是unserialize函數的bug。

不過,好在有人已經解決了,我就借花獻佛,將他記錄下來,供大家使用:

1.假如,是utf-8的資料,請使用下面的函數還原序列化(我沒有驗證,可以嘗試下)

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);
}

2.其他編碼格式的資料,請使用下面的函數還原序列化(數組已驗證,可以使用)

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);
}

希望對各位有點協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.