PHP multiple serialization/deserialization methods

Source: Internet
Author: User
Multiple PHP deserialization methods are used to convert a variable into 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.

 'Apple', 'B' => 'bana', 'C' => 'coconut'); // serialized 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'

'; // 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.

$ 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 called 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

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.

  • $ A = array ('a' => 'apple', 'B' => 'bana', 'C' => 'coconut '); // serialized array $ s = json_encode ($ a); 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.

    $ A = array ('a' => 'apple', 'B' => 'bana', 'C' => 'coconut '); // serialized array $ s = var_export ($ a, true); echo $ s; // output result: array ('a' => 'apple ', 'B' => 'bana', 'C' => 'Coconut',) echo'

    '; // Deserialize 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.

    View source

    Print?

    $ A = array ('a' => 'apple', 'B' => 'bana', 'C' => 'coconut '); // serialized array $ s = wddx_serialize_value ($ a); 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 the 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.

    Contact Us

    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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.