Parse multiple PHP Serialization and deserialization methods. Serialization is the process of converting a variable to a saved or transmitted string. deserialization is to convert the string to the original variable when appropriate. These two processes are combined with serialization to convert a variable to a saved or transmitted string. deserialization is to convert the string to the original variable when appropriate. These two processes can be combined to easily store and transmit data, making the program more maintainability.
1. serialize and unserialize functions
These two are common functions for serialization and deserialization of PHP Data.
The code is as follows:
$ A = array ('a' => 'apple', 'B' => 'bana', 'C' => 'coconut ');
// Serialize the array
$ S = serialize ($ );
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'
';
// Deserialization
$ O = unserialize ($ s );
Print_r ($ o );
// Output result Array ([a] => Apple [B] => banana [c] => Coconut)
?>
When array values contain characters such as double quotes, single quotes, or colons, they may be deserialized. To overcome this problem, a clever technique is to use base64_encode and base64_decode.
The code is as follows:
$ Obj = array ();
// Serialization
$ S = base64_encode (serialize ($ obj ));
// Deserialization
$ Original = unserialize (base64_decode ($ s ));
However, base64 encoding increases the length of the string. To overcome this problem, it can be used with gzcompress.
// Define a function for serializing 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
JSON format serialization and deserialization are a good choice:
The output in json_encode and json_decode formats is much faster than that in serialize and unserialize formats.
The JSON format is readable.
The JSON format is smaller than the data returned by serialize.
JSON format is open and portable. You can also use it in other languages.
The code is as follows:
$ A = array ('a' => 'apple', 'B' => 'bana', 'C' => 'coconut ');
// Serialize the array
$ S = json_encode ($ );
Echo $ s;
// Output result: {"a": "Apple", "B": "banana", "c": "Coconut "}
Echo'
';
// Deserialization
$ O = json_decode ($ s );
In the preceding example, the json_encode output length is obviously 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 variable as PHP code and deserializes it to obtain the content of the original variable.
The code is as follows:
$ A = array ('a' => 'apple', 'B' => 'bana', 'C' => 'coconut ');
// Serialize the array
$ S = var_export ($ a, true );
Echo $ s;
// Output result: array ('a' => 'apple', 'B' => 'bana', 'C' => 'Coconut ',)
Echo'
';
// Deserialization
Eval ('$ my_var ='. $ s .';');
Print_r ($ my_var );
4. wddx_serialize_value and wddx deserialize
The wddx_serialize_value function can serialize array variables and output them as XML strings.
The code is as follows:
$ A = array ('a' => 'apple', 'B' => 'bana', 'C' => 'coconut ');
// Serialize the array
$ S = wddx_serialize_value ($ );
Echo $ s;
// Output result (view the source code of the output string ):
Apple Banana Coconut
Echo'
';
// Deserialization
$ O = wddx_deserialize ($ s );
Print_r ($ o );
// Output result: Array ([a] => Apple [B] => banana 1 => Coconut)
It can be seen that the serialization of this format still occupies a lot of space due to the large number of XML tag characters.
Summary
All the above functions can be normally executed when serializing array variables, but the objects used are different. For example, json_encode fails to serialize the object. When deserializing an object, unserialize and eval have different effects.
Bytes. These two processes are combined...