You'll have more luck finding a routine that can serialize/deserialize objects safely if you use a well-established format like JSON or XML.
But, for the format you've provided, here's a really simple approach, which assumes the keys and values are URI encoded:
PHP Code:
var deserialize = function(s) {
var rv = {};
var pairs = s.split("|");
for (var i in pairs) {
var p = pairs[i].split(":");
if (p.length == 2) {
var k = decodeURIComponent(p[0]);
var v = decodeURIComponent(p[1]);
rv[k] = v;
}
}
return rv;
} // deserialize()
Using the above method, if a key is specified twice, the 2nd value takes precedence. And nested objects are not supported.
Bookmarks