note 101160 deleted from function.session-decode by danbrown

From: Date: Tue, 30 Nov 2010 15:04:16 +0000
Subject: note 101160 deleted from function.session-decode by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-174474@lists.php.net to get a copy of this message
Note Submitter: davyvandenbremt at gmail dot com ---- My go at unserializing the session values: function unserialize_session($val) { $result = array(); // prefixing with semicolon to make it easier to write the regular expression $val = ';' . $val; // regularexpression to find the keys $keyreg = '/;([^|{}"]+)\|/'; // find all keys $matches = array(); preg_match_all($keyreg, $val, $matches); // only go further if we found some keys if (isset($matches[1])) { $keys = $matches[1]; // find the values by splitting the input on the key regular expression $values = preg_split($keyreg, $val); // unshift the first value since it's always empty (due to our semicolon prefix) if (count($values) > 1) { array_shift($values); } // combine the $keys and $values $result = array_combine($keys, $values); } return $result; }

« previous php.notes (#174474) next »