Home > Backend Development > PHP Tutorial > Detailed explanation of PHP serialization and deserialization methods, detailed explanation of PHP serialization and deserialization_PHP tutorial

Detailed explanation of PHP serialization and deserialization methods, detailed explanation of PHP serialization and deserialization_PHP tutorial

WBOY
Release: 2016-07-12 09:06:20
Original
1136 people have browsed it

Detailed explanation of PHP serialization and deserialization methods, detailed explanation of PHP serialization and deserialization

It is often seen that some configuration files store some similar variable names and values ​​​​with formats , in fact, it is a serialization process. When these databases need to be used, a deserialization process will be carried out, which is to restore the string to its original data structure. . Let’s talk about how php serializes and deserializes data .
PHP actually uses two functions to serialize and deserialize data, serialize and unserialize.
serialize Format the array into an ordered string
unserialize Unserialize an array into an array
For example:

$user=array('Moe','Larry','Curly'); 
$user=serialize($stooges); 
echo '<pre class="brush:php;toolbar:false">'; 
print_r($user); 
echo '<br />'; 
print_r(unserialize($user)); 
Copy after login

Result:

a:3:{i:0;s:3:"Moe";i:1;s:5:"Larry";i:2;s:5:"Curly";}
Array ( [0] => Moe [1] => Larry [2] => Curly )
Copy after login

Note that when the array value contains characters such as double quotes, single quotes, colons or Chinese characters, garbled characters or formatting may occur after they are deserialized.

To solve the garbled code problem, you can use the two functions base64_encode and base64_decode .
For example:

$user=array('Moe','Larry','Curly'); 
$user=base64_encode(serialize($user)); 
$user=unserialize(base64_decode($user)); 
Copy after login

This way there will be no garbled code problems, but base64 encoding increases the length of the stored string.

From the above we can summarize an own serialization and deserialization function , as follows:

function my_serialize($obj_array){ 
  return base64_encode(gzcompress(serialize($obj_array))); 
} 
//反序列化 
function my_unserialize($str){ 
  return unserialize(gzuncompress(base64_decode($str))); 
} 
Copy after login

The above is to tell you how PHP serializes and deserializes data, and after encountering deserialization, the reasons and solutions for garbled characters or formatting are disrupted. I hope this article can introduce It will be helpful to everyone’s study.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1065576.htmlTechArticleDetailed explanation of PHP serialization and deserialization methods. Detailed explanation of PHP serialization and deserialization. It is often seen stored in some configuration files. They are some formatted variable names and values, which are actually a sequence...
Related labels:
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