In PHP, Notice: unserialize () [function. unserialize]: Errora appears.

Source: Internet
Author: User
When I used PHP today, I accidentally showed that the unserialize () function conversion always returns false. I confirmed that my string is correct. I tested it many times and it is still the same. No way, enable error_reporting (E_ALL) to enable error information. Notice: unserialize () [function. unserialize]: Erroratoffset Information

When I used PHP today, I accidentally showed that the unserialize () function conversion always returns false. I confirmed that my string is correct. I tested it many times and it is still the same. No way, enable error_reporting (E_ALL) to enable error information. Notice: unserialize () [function. unserialize]: Error at offset Information

When I used PHP today, I accidentally showed that the unserialize () function conversion always returns false. I confirmed that my string is correct. I tested it many times and it is still the same. No way, enable error_reporting (E_ALL) to enable error information. Notice: unserialize () [function. unserialize]: Error at offset information, because I converted the character from an array, so there should be no Error. After Google, it turned out to be an encoding problem, this issue was discussed in the comments of the PHP manual.

In the past, the array string after my serialize was obtained from the database, but when I inserted the database, the view encoding was not saved as UTF8 encoding but edited as ANSCII when I modified the foreground view code, and I copied out with unserialize () PHP file is UTF-8 encoding, encoding is different, so there is an error...

Example:
"The homepage is operated in ANSCII mode.


$ Arr = array ('en' => 'hello', 'cn' => 'Hello ');
$ Str = serialize ($ arr );
Echo $ str;

?>

Output result: a: 2: {s: 2: "en"; s: 5: "hello"; s: 2: "cn"; s: 4: "Hello ";}

Then test it in UTF-8 encoding Mode

The output result of the above Code is: a: 2: {s: 2: "en"; s: 5: "hello"; s: 2: "cn"; s: 6: "Hello ";}
See what's different. The character length is different when a Chinese character appears, that is, strlen ('hello, ') is 4 in the First encoding, the second encoding is 6. For the reason, see the relevant manual. the unsrialize () should judge the character length and Reverse Sequence Based on 6 similar to s: 6: "hello", but the original character length is 4, which is not consistent, that is, the offset error occurs.

Solution:
"Convert the string to be deserialized once. The Code

$ Data = preg_replace ('! S :( \ d + ):"(.*?) ";! Se ', "s:'. strlen ('$ 2').': \" $2 \ "; '", $ data );
$ Data = unserialize ($ data );
If your decoded array contains child elements similar to (double quotation marks transfer characters, you also need to remove repeated transfer double quotation marks (by default, the text editor uploads an image using a slash to escape the URL content, and remove repeated double quotation marks after decoding)

A: 7 {s: 8: "nickname"; s: 10: "4211931411"; s: 3: "uid"; s: 6: "481010"; s: 10: "other_type"; s: 1: "1"; s: 8: "other_id"; I: 2023; s: 11: "other_title"; s: 143: "published a blog post: your uncle in China Cleverly answered the reporter's question"; s: 3: "des"; s: 232 :"


1. Reporter: ye, the mayor of the city was kidnapped. The kidnappers wanted a ransom of 10 million. If they didn't, they burned him to death with gasoline. "; S: 11:" content_url "; s: 53:"/blog/a-log/uid-481010/id-2023 ";}

$ Data ['des '] = stripslashes ($ data ['des']);


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.