Parse PHP for multiple serialization and deserialization methods

  • 2020-06-07 04:05:08
  • OfStack

Serialization is the process of converting a variable into a string that can be saved or transferred; Deserialization is the process of converting the string back to its original variable when appropriate. Together, these two processes make it easy to store and transfer data, making the program more maintainable.
serialize and unserialize functions
These two are common functions for serializing and deserializing data in PHP.

<?php
$a = array('a'=> 'Apple' ,'b' => 'banana', 'c' => 'Coconut');
// Serialized array 
$s = serialize($a);
echo $s;
// Output results: a:3:{s:1:"a";s:5:"Apple";s:1:"b";s:6:"banana";s:1:"c";s:7:"Coconut";}
echo '<br /><br />';
// deserialization 
$o = unserialize($s);
print_r($o);
// The output  Array ( [a] => Apple [b] => banana [c] => Coconut )
?>

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

$obj = array();
// serialization 
$s = base64_encode(serialize($obj));
// deserialization 
$original = unserialize(base64_decode($s));
 but base64 The encoding increases the length of the string. To overcome this problem, we can sum gzcompress1 Use. 
// define 1 A function that serializes objects 
function my_serialize($obj )
{
   returnbase64_encode(gzcompress(serialize($obj)));
}
// deserialization 
function my_unserialize($txt)
{
   returnunserialize(gzuncompress(base64_decode($txt)));
}

2. json_encode and json_decode
The JSON format for serialization and deserialization is a good choice:
Output is much faster using json_encode and json_decode formats.
The JSON format is readable.
The JSON format returns smaller results than the serialize format.
The JSON format is open and portable. Other languages can also use it.

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

// Serialized array 
$s = json_encode($a);
echo $s;
// Output results: {"a":"Apple","b":"banana","c":"Coconut"}
echo '<br /><br />';
// deserialization 
$o = json_decode($s);

In the above example, the json_encode output length is clearly shorter than the serialize output length in the previous example.
3. var_export and eval
The var_export function outputs the variable as a string; eval executes the string as if it were PHP code and deserializes the contents of the original variable.

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

// Serialized array 
$s = var_export($a, true);
echo $s;
// Output results:  array ( 'a' => 'Apple', 'b' => 'banana', 'c' => 'Coconut', )
echo '<br /><br />';
// deserialization 
eval('$my_var='. $s . ';');
print_r($my_var);

4. wddx_serialize_value and wddx deserialize
The wddx_serialize_value function serializes array variables and outputs them as XML strings.

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

// Serialized array 
$s = wddx_serialize_value($a);
echo $s;
// Output results (see the source 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 />';
// deserialization 
$o = wddx_deserialize($s);
print_r($o);
// Output results: Array ( [a] => Apple [b] => banana 1 => Coconut )

As you can see, the XML tag has a lot of characters, so the serialization of this format still takes up a lot of space.
summary
All of the above functions work fine when serializing array variables, but not when applied to objects. For example, json_encode will fail to serialize the object. unserialize and eval will have different effects when deserializing objects.

Related articles: