Javascript PHP Serializer
Author: Morten Amundsen
Submission Date: 2006-05-03
Website:
Email: mor10am@gmail.com
Keywords: javascript, php, multi-byte characters, arrays, serializer, unserialize
Synopsis: Be warned that Javascript is "smarter" than PHP when multibyte characters are involved. Javascript PHP Serializer
Submission Date: 2006-05-03
Website:
Email: mor10am@gmail.com
Keywords: javascript, php, multi-byte characters, arrays, serializer, unserialize
Synopsis: Be warned that Javascript is "smarter" than PHP when multibyte characters are involved. Javascript PHP Serializer
Be warned that Javascript is "smarter" than PHP when multibyte characters are involved.
SomeString.length Will tell you the number of characters in a string, no the number of bytes. The example here will only work if the characters in the string are all single byte (i.e. if you may have problems with UTF-8), because PHP's unserialize(), like most PHP string functions, regard 1 char = 1 byte.
/*
* PHP Serialize
* Morten Amundsen
* mor10am@gmail.com
*/
function php_serialize(obj)
{
var string = \'\';
if (typeof(obj) == \'object\') {
if (obj instanceof Array) {
string = \'a:\';
tmpstring = \'\';
count = 0;
for (var key in obj) {
tmpstring = php_serialize(key);
tmpstring = php_serialize(obj[key]);
count ;
}
string = count \':{\';
string = tmpstring;
string = \'}\';
} else if (obj instanceof Object) {
classname = obj.toString();
if (classname == \'[object Object]\') {
classname = \'StdClass\';
}
string = \'O:\' classname.length \':\"\' classname \'\":\';
tmpstring = \'\';
count = 0;
for (var key in obj) {
tmpstring = php_serialize(key);
if (obj[key]) {
tmpstring = php_serialize(obj[key]);
} else {
tmpstring = php_serialize(\'\');
}
count ;
}
string = count \':{\' tmpstring \'}\';
}
} else {
switch (typeof(obj)) {
case \'number\':
if (obj - Math.floor(obj) != 0) {
string = \'d:\' obj \';\';
} else {
string = \'i:\' obj \';\';
}
break;
case \'string\':
string = \'s:\' obj.length \':\"\' obj \'\";\';
break;
case \'boolean\':
if (obj) {
string = \'b:1;\';
} else {
string = \'b:0;\';
}
break;
}
}
return string;
}
Link to this article:
http://www.daremedy.com/tutorials/js/Javascript-tutorial-18.html
