Multiple PHP Serialization and deserialization methods
Source: Internet
Author: User
This article provides a detailed analysis of multiple serialization and deserialization methods in PHP. For more information about serialization, see the process of converting variables into saved or transmitted strings; 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 ');
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'
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.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.