Home > Backend Development > PHP Problem > How to solve the problem of Chinese garbled characters in serialize function in PHP

How to solve the problem of Chinese garbled characters in serialize function in PHP

WBOY
Release: 2023-03-15 16:10:02
Original
2506 people have browsed it

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.

How to solve the problem of Chinese garbled characters in serialize function in PHP

The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.

How to solve the problem of Chinese garbled characters in the serialize function in php

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 )
Copy after login

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(&#39;Google&#39;, &#39;Runoob&#39;, &#39;Facebook&#39;);
$serialized_data = serialize($sites);
echo  $serialized_data . PHP_EOL;
?>
Copy after login

Output result:

How to solve the problem of Chinese garbled characters in serialize function in PHP

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template