note 74932 added to function.unserialize

From: Date: Fri, 04 May 2007 18:02:29 +0000
Subject: note 74932 added to function.unserialize
Groups: php.notes 
Request: Send a blank email to php-notes+get-126202@lists.php.net to get a copy of this message
If instead of using JSON, you'd like to stick with PHP-style serialization, here's some JavaScript code I posted at http://magnetiq.com for serializing JavaScript objects in PHP fashion: /* Returns the class name of the argument or undefined if it's not a valid JavaScript object. */ function getObjectClass(obj) { if (obj && obj.constructor && obj.constructor.toString) { var arr = obj.constructor.toString().match( /function\s*(\w+)/); if (arr && arr.length == 2) { return arr[1]; } } return undefined; } /* Serializes the given argument, PHP-style. The type mapping is as follows: JavaScript Type PHP Type --------------- -------- Number Integer or Decimal String String Boolean Boolean Array Array Object Object undefined Null The special JavaScript object null also becomes PHP Null. This function may not handle associative arrays or array objects with additional properties well. */ function phpSerialize(val) { switch (typeof(val)) { case "number": return (Math.floor(val) == val ? "i" : "d") + ":" + val + ";"; case "string": return "s:" + val.length + ":\"" + val + "\";"; case "boolean": return "b:" + (val ? "1" : "0") + ";"; case "object": if (val == null) { return "N;"; } else if ("length" in val) { var idxobj = { idx: -1 }; return "a:" + val.length + ":{" + val.map( function (item) { this.idx++; var ser = phpSerialize(item); return ser ? phpSerialize(this.idx) + ser : false; }, idxobj).filter( function (item) { return item; }).join("") + "}"; } else { var class_name = getObjectClass(val); if (class_name == undefined) { return false; } var props = new Array(); for (var prop in val) { var ser = phpSerialize(val[prop]); if (ser) { props.push(phpSerialize(prop) + ser); } } return "O:" + class_name.length + ":\"" + class_name + "\":" + props.length + ":{" + props.join("") + "}"; } case "undefined": return "N;"; } return false; } On the client side, you can pass in a complex (nested) JavaScript object to the phpSerialize function to get a PHP-style serialized representation. This string can be posted back and directly passed to the unserialize function to yield a representation of the complex object in PHP realm. Use of this technique requires caution on security matters. ---- Server IP: 216.235.15.211 Probable Submitter: 209.205.16.132 ---- Manual Page -- http://www.php.net/manual/en/function.unserialize.php Edit -- https://master.php.net/note/edit/74932 Del: integrated -- https://master.php.net/note/delete/74932/integrated Del: useless -- https://master.php.net/note/delete/74932/useless Del: bad code -- https://master.php.net/note/delete/74932/bad+code Del: spam -- https://master.php.net/note/delete/74932/spam Del: non-english -- https://master.php.net/note/delete/74932/non-english Del: in docs -- https://master.php.net/note/delete/74932/in+docs Del: other reasons-- https://master.php.net/note/delete/74932 Reject -- https://master.php.net/note/reject/74932 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#126202) next »