Php serialization and deserialization test in utf8 and gbk encoding-PHP source code

Source: Internet
Author: User
In php, if we use unified encoding, there is no problem, but many friends will find that the values returned by utf8 and gbk encoding are different, the following is a compilation of tutorials to introduce some of their problems. In php, if we use unified encoding, there is no problem, but many friends will find that the values returned by utf8 and gbk encoding are different, the following is a compilation of tutorials to introduce some of their problems.

Script ec (2); script

Php uses serialize and unserialize for mutual serialization and deserialization in utf8 and gbk encoding, which may cause the problem that the deserialization fails.
The cause of the problem is that the strlen function calculates the length of a Chinese string in different encodings.

The Code is as follows:
$ Array = array ('title' => 'php tutorial sharing net', 'url' => 'HTTP: // www.111cn.net ');
Echo serialize ($ array );
// Gbk encoding a: 2: {s: 5: "title"; s: 13: "php tutorial Share Network"; s: 3: "url"; s: 20: "http://www.111cn.net ";}
// Utf8 encoding a: 2: {s: 5: "title"; s: 18: "php tutorial Share Network"; s: 3: "url"; s: 20: "http://www.111cn.net ";}
?>

To solve this problem, we need to re-fix the length of the string during deserialization.
Solution

The Code is as follows:

$ Str = 'a: 2: {s: 5: "title"; s: 13: "php tutorial Share Network"; s: 3: "url"; s: 20: "http://www.111cn.net ";}';
$ Regex = '/s \ :( \ d +) \: \ "([^ \"] +) \ "/isx ';

$ Str = preg_replace_callback (

$ Regex,

"Fixser ",

$ Str );

Function fixser ($ matches)

{

Return's: '. strlen ($ matches [2]).': '.' "'. $ matches [2].'" ';

}

?>


You can change it to an anonymous function.

The Code is as follows:

$ Str = 'a: 2: {s: 5: "title"; s: 13: "php tutorial Share Network"; s: 3: "url"; s: 20: "http://www.111cn.net ";}';
$ Regex = '/s \ :( \ d +) \: \ "([^ \"] +) \ "/isx ';

$ Str = preg_replace_callback (
$ Regex,
Function ($ matches)
{
Return's: '. strlen ($ matches [2]).': '.' "'. $ matches [2].'" ';
},
$ Str );

?>

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.