Javascript->PHP serializer
6
Fordiman
This is a quick set of overrides for Javascript so that any variable can be passed to PHP in a GET/POST activity. Just call myVar.toPHP();
I use this little set of functions extensively in a little Javascript/PHP RPC handler I wrote. I don't have the reverse function, as I pass JSON back to the browser for the return value.
Note: This lib is not safe for binaries or HTML Elements. The former will come out similar to FTP ASCII breaks, and the latter will cause infinite recursion. If you want to make a speical case for HTML Elements, do so; you could probably just test for parentNode and create a 'safe' object from that. I didn't need it, so I didn't code it.
As for binary safety, at some point between toPHP/escape/post/urldecode/unserialize, the object breaks. Rather than create a huge fix for something I didn't need to do, I put in a quick match/hack. Don't like it? Write the fix yourself.
I use this little set of functions extensively in a little Javascript/PHP RPC handler I wrote. I don't have the reverse function, as I pass JSON back to the browser for the return value.
Note: This lib is not safe for binaries or HTML Elements. The former will come out similar to FTP ASCII breaks, and the latter will cause infinite recursion. If you want to make a speical case for HTML Elements, do so; you could probably just test for parentNode and create a 'safe' object from that. I didn't need it, so I didn't code it.
As for binary safety, at some point between toPHP/escape/post/urldecode/unserialize, the object breaks. Rather than create a huge fix for something I didn't need to do, I put in a quick match/hack. Don't like it? Write the fix yourself.
Object.prototype.toPHP = function () {
var retf='O:8:"stdClass":';
var reta=':{';
var ct=0;
var last;
for (var i in this) {
if (typeof this[i] == 'function') continue;
reta+=i.toPHP()+this[i].toPHP();
ct++;
}
return retf+ct+reta+'}';
}
String.prototype.toPHP = function () {
/*
Make sure all strings have network-style carraige
returns; if we don't, PHP doesn't recognize the
length as correct. As a result, the String type
is not binary safe. What are you doing passing
binaries via POST anyway?? Use the normal file
uploading for cripes sake.
*/
var s=this.replace(/((?:\r|\n|\r\n))/g,'\r\n');
var i=-1,ct=0;
while ((i=s.indexOf('\n',i+1))!=-1) ct++;
return 's:'+(s.length-ct)+':"'+s+'";';
}
Boolean.prototype.toPHP = function () {
return 'b:'+(this==true?1:0)+';';
}
Number.prototype.toPHP = function () {
/* Only make it a double if we actually need to */
return (this==Math.floor(this)?'i':'d')+':'+this.toString()+';';
}
Date.prototype.toPHP = function () {
/* Changes into an int compatible with mktime() */
return 'i:'+Math.floor(this.getTime()/1000)+';';
}
Array.prototype.toPHP = function () {
var ret='a:'+this.length+':{';
for (var i=0; i<this.length; i++)
ret+=i.toPHP()+this[i].toPHP();
return ret+'}';
}
Function.prototype.toPHP = function () {
return null;
}






Meanwhile, if you understood what it's for, no example should be needed, other than:
var X = { this:'is', a:['test','testing'],one:2,three:4.5 }
console.log(X.toPHP());
Output:
a:4:{s:1:"s";s:2:"is";s:1:"a";a:2:{i:0;s:4:"test";i:1;s:7:"testing";}s:3:"one";i:2;s:5:"three";d:4.5;}
Visit us at http://www.stutimandal.com
Regards,
Kumar S
GuyFromChennai.com