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