PHP Tutorial - Deserialization Method




Serialization is the process of converting a variable into a string that can be saved or transmitted; deserialization is the process of converting the string back into the original variable when appropriate. These two processes combine to store and transfer data easily, making the program more maintainable. Brothers PHP training (www.lampbrother.net)



1. serialize and unserialize functions



These two are commonly used functions for serializing and deserializing data in PHP.

<?php

$a = array('a' => 'Apple' ,'b' => 'banana' , 'c' => 'Coconut');

//serialize array

$s = serialize($a);

echo $s;

//Output result: a:3:{s:1:"a";s:5:"Apple";s:1:"b";s:6:"banana";s:1: "c";s:7:"Coconut";}

echo '<br /><br />';

//Deserialize

$o = unserialize($s);

print_r($o);

//Output result Array ( [a] => Apple [b] => banana [c] => Coconut )

?>

Problems can arise when array values ​​contain characters such as double quotes, single quotes, or colons after they are deserialized. To overcome this, a neat trick is to use base64_encode and base64_decode.

$obj = array();

//Serialization

$s = base64_encode(serialize($obj));

//Deserialize

$original = unserialize(base64_decode($s));

but base64 encoding will increase the length of the string. To overcome this, use with gzcompress.

//Define a function to serialize objects

function my_serialize( $obj )

{

   return base64_encode(gzcompress(serialize($obj)));

}

//Deserialize

function my_unserialize($txt)

{

   return unserialize(gzuncompress(base64_decode ($txt)));

}

2. json_encode and json_decode



use JSON format to serialize and deserialize is a good choice:



using json_encode and json_decode format output is much faster to serialize and unserialize format.

JSON format is readable.

JSON format is smaller than serialize return data result.

JSON format is open and portable. Other languages ​​can also use it.

$a = array('a' => 'Apple' ,'b' => 'banana' , 'c' => 'Coconut');

//serialize array

$s = json_encode($a);

echo $s ;

//Output result: {"a":"Apple","b":"banana","c":"Coconut"}

echo '<br /><br />';

//Deserialize

$o = json_decode($s);

In the above example, the output length of json_encode is obviously shorter than the output length of serialize in the previous example.



3. var_export and eval



The var_export function outputs the variable as a string; eval executes the string as PHP code, and deserializes to get the original variable content.



$a = array('a' => 'Apple' ,'b' => 'banana' , 'c' => 'Coconut');



// serialize array



$s = var_export($a , true);



echo $s;



//Output result: array ( 'a' =>















print_r($my_var);

4. wddx_serialize_value and wddx deserialize The



wddx_serialize_value functions can serialize array variables and output them as XML strings.



$a = array('a' => 'Apple' ,'b' => 'banana' , 'c' => 'Coconut');



//serialize array

$s = wddx_serialize_value($a);

echo $s ;



//Output result (check the source code of the output string): <wddxPacket version='1.0'><header/><data><struct><var name='a'><string>Apple</string></ var><var name='b'><string>banana</string></var><var name='c'><string>Coconut</string></var></struct></data> </wddxPacket>



echo '<br /><br />';



//Deserialize

$o = wddx_deserialize($s);

print_r($o);

//Output result: Array ( [a] => Apple [b] => banana 1 => Coconut )

It can be seen that the XML tag has many characters, which leads to the serialization of this format still taking up Lots of space.



summary



All of the above functions work fine when serializing array variables, but it's different when applied to objects. For example, json_encode serializing objects will fail. When deserializing objects, unserialize and eval will have different effects.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326429530&siteId=291194637