note 93389 deleted from function.unserialize by aharvey

From: Date: Tue, 06 Nov 2012 01:36:31 +0000
Subject: note 93389 deleted from function.unserialize by aharvey
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-191723@lists.php.net to get a copy of this message
Note Submitter: TBag ---- 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; } ?>

« previous php.notes (#191723) next »