note 93389 modified in function.unserialize by danbrown

From: Date: Thu, 17 Sep 2009 14:50:32 +0000
Subject: note 93389 modified in function.unserialize by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-160785@lists.php.net to get a copy of this message
My first post on php.net. Hope someone finds it helpfull. These few functions will serialize and unserialize arrays and objects. It works recursively too. I call them "might_..." because they test to see if any action is required. <?php function might_serialize($val){ if(is_object($val)){ $obj_keys = array_keys(get_object_vars($val)); foreach($obj_keys as $k){ $val->$k = might_serialize($val->$k); } $val = serialize($val); } if(is_array($val)){ foreach($val as &$v){ $v = might_serialize($v); } $val = serialize($val); } return $val; } function might_unserialized($val){ //$pattern = "/.*\{(.*)\}/"; if(is_serialized($val)){ $ret = unserialize($val); foreach($ret as &$r){ $r = might_unserialized($r); } return $ret; }else { return $val; } } function is_serialized($val){ if (!is_string($val)){ return false; } if (trim($val) == "") { return false; } //if (preg_match("/^(i|s|a|o|d)(.*);/si",$val)) { return true; } if (preg_match("/^(i|s|a|o|d):(.*);/si",$val) !== false) { return true; } return false; } ?> --was-- My first post on php.net. Hope someone finds it helpfull. These few functions will serialize and unserialize arrays and objects. It works recursively too. I call them "might_..." because they test to see if any action is required. <?php function might_serialize($val){ if(is_object($val)){ $obj_keys = array_keys(get_object_vars($val)); foreach($obj_keys as $k){ $val->$k = might_serialize($val->$k); } $val = serialize($val); } if(is_array($val)){ foreach($val as &$v){ $v = might_serialize($v); } $val = serialize($val); } return $val; } function might_unserialized($val){ //$pattern = "/.*\{(.*)\}/"; if(is_serialized($val)){ $ret = unserialize($val); foreach($ret as &$r){ $r = might_unserialized($r); } return $ret; }else { return $val; } } function is_serialized($val){ if (!is_string($val)){ return false; } if (trim($val) == "") { return false; } //if (preg_match("/^(i|s|a|o|d)(.*);/si",$val)) { return true; } if (preg_match("/^(i|s|a|o|d):(.*);/si",$val)) { return true; } return false; } ?> http://php.net/manual/en/function.unserialize.php

« previous php.notes (#160785) next »