PHP unserialize function Anti-sequence session array

Source: Internet
Author: User

There is no, encountered this situation: using the Serialize function to serialize an array, and then want to pass the Unserialize function to reverse order, get the array, but the error, not get the desired result.

Search from the Internet, found that many netizens have encountered the same problem, it seems that this is the unserialize function of the bug.

However, fortunately someone has solved, I will jiehuaxianfo, record him for everyone to use:

1. If the data is Utf-8, use the following function to deserialize (I have no validation, can try)

function mb_unserialize ($serial _str) {
$serial _str= preg_replace ('!s: (\d+): "(. *?)";! Se ', ' s: '. strlen (' $ "). ': \ ' $2\ "; '", $serial _str);
$serial _str= str_replace ("\ R", "", $serial _str);
Return Unserialize ($serial _str);
}

2. Other encoded format data, use the following function to deserialize (the array has been validated, can be used)

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

Hope to you a little help!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.