Posted By

evinweissenberg on 08/23/12


Tagged

php base64 serialize


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

tux-world


Serializer


 / Published in: PHP
 

URL: http://www.evinw.com

Correct way to serialize array or object using base64 encode and UTF8 enforcing.

  1. <?php
  2. /*
  3. |---------------------------
  4. | Author: Evin Weissenberg
  5. |---------------------------
  6. */
  7. class Serialize {
  8.  
  9. private $subject; //object or array
  10. private $utf8;
  11.  
  12. const CHARSET = 'default_charset';
  13. const ENCODING = 'UTF-8';
  14.  
  15. private function __constructor() {
  16.  
  17. $this->utf8 = ini_set(self::CHARSET, self::ENCODING);
  18.  
  19. }
  20.  
  21. public function setSubject($subject) {
  22.  
  23. $this->subject = $subject;
  24. return $this;
  25.  
  26. }
  27.  
  28. public function serialize() {
  29.  
  30. $serialize = base64_encode(serialize($this->subject));
  31. return $serialize;
  32.  
  33. }
  34.  
  35. public function unSerialize() {
  36.  
  37. $un_serialize = base64_decode(unserialize($this->subject));
  38. return $un_serialize;
  39.  
  40. }
  41.  
  42. private function __destructor() {
  43.  
  44. unset($this->subject);
  45. unset($this->utf8);
  46.  
  47. }
  48. }
  49. //Usage
  50. $array=array('Car','red','Boat'=>'white');
  51. $obj = new Serialize();
  52. $serialize = $obj->setSubject($array)->serialize();
  53. print_r($serialize);

Report this snippet  

You need to login to post a comment.