Home > Article > Backend Development > How to solve the problem of Chinese garbled characters in serialize function in PHP
PHP's method to solve the problem of Chinese garbled characters in the serialize function: perform "base64_encode" encoding on the string containing Chinese after serialization. After encoding, the special characters can be escaped and the Chinese characters can be transmitted to the client. end.
The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.
The serialize() function is used to serialize objects or arrays and return a string.
serialize() function After serializing the object, it can be easily passed to other places that need it, and its type and structure will not change.
The syntax is:
string serialize ( mixed $value )
Parameter description:
$value: The object or array to be serialized.
I believe that everyone will encounter the problem that Chinese cannot be parsed when using serialize to encode Chinese. Especially when transmitting serialized Chinese strings to the client, how should we solve this problem?
In fact, it is very easy. Base64_encode the serialized string containing Chinese characters again, so that the special characters can be transferred. Chinese characters are transmitted to the client.
The example is as follows:
<?php $sites = array('Google', 'Runoob', 'Facebook'); $serialized_data = serialize($sites); echo $serialized_data . PHP_EOL; ?>
Output result:
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to solve the problem of Chinese garbled characters in serialize function in PHP. For more information, please follow other related articles on the PHP Chinese website!