Use PHP functions in JavaScript

JavaScript serialize

Returns a string representation of variable (which can later be unserialized)

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
6566
67
68
69
7071
72
73
74
7576
77
78
79
8081
82
83
84
8586
87
88
89
9091
92
93
94
9596
97
98
99
100101
102
103
function serialize (mixed_value) {
    // Returns a string representation of variable (which can later be unserialized)  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/serialize    // +   original by: Arpad Ray (mailto:arpad@php.net)
    // +   improved by: Dino
    // +   bugfixed by: Andrej Pavlovic
    // +   bugfixed by: Garagoth
    // +      input by: DtTvB (http://dt.in.th/2008-09-16.string-length-in-bytes.html)    // +   bugfixed by: Russell Walker (http://www.nbill.co.uk/)
    // +   bugfixed by: Jamie Beck (http://www.terabit.ca/)
    // +      input by: Martin (http://www.erlenwiese.de/)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // -    depends on: utf8_encode    // %          note: We feel the main purpose of this function should be to ease the transport of data between php & js
    // %          note: Aiming for PHP-compatibility, we have to translate objects to arrays
    // *     example 1: serialize(['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: 'a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}'
    // *     example 2: serialize({firstName: 'Kevin', midName: 'van', surName: 'Zonneveld'});    // *     returns 2: 'a:3:{s:9:"firstName";s:5:"Kevin";s:7:"midName";s:3:"van";s:7:"surName";s:9:"Zonneveld";}'
    var _getType = function (inp) {
        var type = typeof inp, match;
        var key;
        if (type == 'object' && !inp) {            return 'null';
        }
        if (type == "object") {
            if (!inp.constructor) {
                return 'object';            }
            var cons = inp.constructor.toString();
            match = cons.match(/(\w+)\(/);
            if (match) {
                cons = match[1].toLowerCase();            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {
                if (cons == types[key]) {
                    type = types[key];                    break;
                }
            }
        }
        return type;    };
    var type = _getType(mixed_value);
    var val, ktype = '';
    
    switch (type) {        case "function": 
            val = ""; 
            break;
        case "boolean":
            val = "b:" + (mixed_value ? "1" : "0");            break;
        case "number":
            val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
            break;
        case "string":            mixed_value = this.utf8_encode(mixed_value);
            val = "s:" + encodeURIComponent(mixed_value).replace(/%../g, 'x').length + ":\"" + mixed_value + "\"";
            break;
        case "array":
        case "object":            val = "a";
            /*
            if (type == "object") {
                var objname = mixed_value.constructor.toString().match(/(\w+)\(\)/);
                if (objname == undefined) {                    return;
                }
                objname[1] = this.serialize(objname[1]);
                val = "O" + objname[1].substring(1, objname[1].length - 1);
            }            */
            var count = 0;
            var vals = "";
            var okey;
            var key;            for (key in mixed_value) {
                ktype = _getType(mixed_value[key]);
                if (ktype == "function") { 
                    continue; 
                }                
                okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key);
                vals += this.serialize(okey) +
                        this.serialize(mixed_value[key]);
                count++;            }
            val += ":" + count + ":{" + vals + "}";
            break;
        case "undefined": // Fall-through
        default: // if the JS object has a property which contains a null value, the string cannot be unserialized by PHP            val = "N";
            break;
    }
    if (type != "object" && type != "array") {
        val += ";";    }
    return val;
}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

1
serialize(['Kevin', 'van', 'Zonneveld']);

Should return

1
'a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}'

» Example 2

Running

1
serialize({firstName: 'Kevin', midName: 'van', surName: 'Zonneveld'});

Should return

1
'a:3:{s:9:"firstName";s:5:"Kevin";s:7:"midName";s:3:"van";s:7:"surName";s:9:"Zonneveld";}'

Dependencies

In order to use this function, you also need:

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have serialize goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Fadil Kujundzic
Aug 12th Permalink

q  I had a problem with serialize function objects were stored as array. To fix this I changed at line 73 [CODE] objname [1] this.serialize (= objname [1 ]);[/ CODE] to [CODE] objname [1] this.serialize (= objname [1] == "Object [" ? "stdClass": objname [1 ]);[/ CODE]. Maybe might be useful for someone.

Gravatar
Le Torbi
26 Sep '09 Permalink

q  Hi there,

when fixing the UTF-8 issue of the unserialize() function I've found a way to improve the speed of the size calculation for strings. It's quite simple and need no complex string operations or regular expressions. Here is the code:

var utf8Size = function(str) { // NEW FUNCTION
    var size = 0;
    for (var i = 0; i < str.length; i++) {
        var code = str[i].charCodeAt(0);
        if (code < 0x0080)
            size += 1;
        else if (code < 0x0800)
            size += 2;
        else
        size += 3;
    }
    return size;
}
var _getType = function (inp) {
    var type = typeof inp, match;
    var key;
// MORE LINES OF CODE
        val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
        break;
    case "string":
        val = "s:" + utf8Size(mixed_value) + ":"" + mixed_value + """; // MODIFIED LINE
        break;
 case "array":
case "object":



I've made some simple test and it seems that my function needs about 0.0004ms per run, while the old needs 0.004ms. Ok, it's not much, but maybe worth the code anyway...

BTW: What do I have to to to get this into the official code?

Bai
Le Torbi

Gravatar
Brett Zamir
19 Aug '09 Permalink

q  @Russell Walker: That sounds good, but can you please confirm that this is in fact the same behavior in PHP?

Gravatar
Russell Walker
18 Aug '09 Permalink

q  When serializing strings that contain URL entities (such as the plus symbol), they were being lost during unserialization in PHP. To fix this, I changed line 58 to URIEncode the string value like this:

val = "s:" + encodeURIComponent(mixed_value).replace(/%../g, 'x').length + ":"" + encodeURIComponent(mixed_value) + """;

Gravatar
Kevin van Zonneveld
16 Aug '09 Permalink

q  @ Alexandre Felipe Muller: Thanks for sharing. It's gonna take a while to investigate it and strip it of your environment's specific dependencies like showMessage(Element('cc_msg_err_serialize_data_unknown').value);
If after that all the testcases pass and they're indeed faster I will replace the current implementations with your's

Gravatar
Alexandre Felipe Muller
5 Aug '09 Permalink

q  We're using my serialize and unserialize in my project for 3 years, acording to my tests it's 3 or 4 times faster. Who want to see

	cConnector.prototype.serialize = function(data)
	{	var _thisObject = this;		
		var f = function(data)
		{
			var str_data;
	
			if (data == null || 
				(typeof(data) == 'string' && data == ''))
			{
				str_data = 'N;';
			}
	
			else switch(typeof(data))
			{
				case 'object':
					var arrayCount = 0;
	
					str_data = '';
	
					for (i in data)
					{
						if (i == 'length')
						{
							continue;
						}
						
						arrayCount++;
						switch (typeof(i))
						{
							case 'number':
								str_data += 'i:' + i + ';' + f(data[i]);
								break;
	
							case 'string':
								str_data += 's:' + i.length + ':"' + i + '";' + f(data[i]);
								break;
	
							default:
								showMessage(Element('cc_msg_err_serialize_data_unknown').value);
								break;
						}
					}
	
					if (!arrayCount)
					{
						str_data = 'N;';	
					}
					else
					{
						str_data = 'a:' + arrayCount + ':{' + str_data + '}';
					}
					
					break;
			
				case 'string':
					str_data = 's:' + data.length + ':"' + data + '";';
					break;
					
				case 'number':
					str_data = 'i:' + data + ';';
					break;
	
				case 'boolean':
					str_data = 'b:' + (data ? '1' : '0') + ';';
					break;
	
				default:
					showMessage(Element('cc_msg_err_serialize_data_unknown').value);
					return null;
			}

			return str_data;
		}
	
		return f(data);
	}
	//Unserialize Data Method
	cConnector.prototype.unserialize = function(str)
	{
		_thisObject = this;
		var matchB = function (str, iniPos)
		{
			var nOpen, nClose = iniPos;
			do
			{
				nOpen = str.indexOf('{', nClose+1);
				nClose = str.indexOf('}', nClose+1);

				if (nOpen == -1)
				{
					return nClose;
				}
				if (nOpen < nClose )
				{
					nClose = matchB(str, nOpen);
				}
			} while (nOpen < nClose);

			return nClose;
		}

		var f = function (str)
		{
			switch (str.charAt(0))
			{
				case 'a':
					var data = new Array();
					var n = parseInt( str.substring(str.indexOf(':')+1, str.indexOf(':',2) ) );
					var arrayContent = str.substring(str.indexOf('{')+1, str.lastIndexOf('}'));
					for (var i = 0; i < n; i++)
					{
						var pos = 0;
	
						/* Process Index */
						var indexStr = arrayContent.substr(pos, arrayContent.indexOf(';')+1);
						var index = f(indexStr);
						pos = arrayContent.indexOf(';', pos)+1;
						
						/* Process Content */
						var part = null;
						switch (arrayContent.charAt(pos))
						{
							case 'a':
								var pos_ = matchB(arrayContent, arrayContent.indexOf('{', pos))+1;
								part = arrayContent.substring(pos, pos_);
								pos = pos_;
								data[index] = f(part);
								break;
						
							case 's':
								var pval = arrayContent.indexOf(':', pos+2);
								var val  = parseInt(arrayContent.substring(pos+2, pval));
								pos = pval + val + 4;
								data[index] = arrayContent.substr(pval+2, val);
								break;
	
							default:
								part = arrayContent.substring(pos, arrayContent.indexOf(';', pos)+1);
								pos = arrayContent.indexOf(';', pos)+1;
								data[index] = f(part);
								break;
						}
						arrayContent = arrayContent.substr(pos);
					}
					break;
					
				case 's':
					var pos = str.indexOf(':', 2);
					var val = parseInt(str.substring(2,pos));
					var data = str.substr(pos+2, val);
					str = str.substr(pos + 4 + val);
					break;
	
				case 'i':
				case 'd':
					var pos = str.indexOf(';');
					var data = parseInt(str.substring(2,pos));
					str = str.substr(pos + 1);
					break;
				
				case 'N':
					var data = null;
					str = str.substr(str.indexOf(';') + 1);
					break;
	
				case 'b':
					var data = str.charAt(2) == '1' ? true : false;
					break;
			}
			return data;
		}
	
		return f(str);
	}

Gravatar
Brett Zamir
3 Aug '09 Permalink

q  @Jamie: Fixed in SVN. Thanks for the report!

Gravatar
Jamie Beck
3 Aug '09 Permalink

q  Should not lines 83-84 be as follows for the namespaced version. Otherwise the function cannot be found...

vals += this.serialize(okey) +
    this.serialize(mixed_value[key]);

Gravatar
Brett Zamir
18 Jun '09 Permalink

q  Fixed in SVN! Thanks! (also added your website to the credit)

Gravatar
Russell Walker
16 Jun '09 Permalink

q  I found that if the javascript object has a property which contains a null value, the string cannot be unserialized by PHP. To fix this, I added:

default:
  val = "N";
  break;



...to the end of the switch block (around line 92 on the above function). The 'undefined' case should probably be moved down to the bottom as well so both can be handled together, ie:


case 'undefined': default: val = "N"; break;

Gravatar
Kevin van Zonneveld
31 May '09 Permalink

q  @ Russell Walker: Thanks for contributing. I've implemented your fix in svn & it will be available shortly.
http://trac.plutonia.nl/projects/phpjs/browser/trunk/functions/var/serialize.js

Gravatar
Russell Walker
30 May '09 Permalink

q  Sorry, I quoted the line number from my own file, but in php.default.js, it is line 5826.

Gravatar
Russell Walker
30 May '09 Permalink

q  I found that when serializing utf-8 characters that differ from iso-8859-1, the string could not be deserialized by PHP. This is because PHP sees the string as containing more characters than it really does (as it thinks 1 character = 1 byte, when unicode characters can take up more than 1 byte). So I amended the code on line 97 from

val = "s:" + mixed_value.length + ":"" + mixed_value + """;



to

val = "s:" + encodeURIComponent(mixed_value).replace(/%../g, 'x').length + ":"" + mixed_value + """;



...so now it serializes utf-8 characters in a way that PHP can deserialize. Note however, that this will probably break the javascript unserialize function, as JS and PHP cannot agree on the number of characters in the string.

The code to get the length of the string in bytes came from DtTvB: http://dt.in.th/2008-09-16.string-length-in-bytes.html

Gravatar
Kevin van Zonneveld
12 May '09 Permalink

q  @ AndreaZ: Thx for the pastebin, this makes it clear to me what's going wrong. You are stripping slashes before you are unserializing the string, while escaped characters are an essential part of of the serialized object.

To circumvent, remove stripslashes, or first use base64_encode over the serialized object, and then in php decode it.

Gravatar
AndreaZ
1 May '09 Permalink

q  @Kevin: http://pastebin.com/m7f1e9ef0

Gravatar
Kevin van Zonneveld
29 Apr '09 Permalink

q  @ AndreaZ: Why don't you put it in pastebin.org, and add a link here. then we have syntax highlighting as well. Thanx!

Gravatar
AndreaZ
25 Apr '09 Permalink

q  @Kevin: i inserted the code inside

but it seems that i wrong something :-(

send me an email, so i can send you the test file

Gravatar
AndreaZ
25 Apr '09 Permalink

q  @Kevin: that's the test code

<html>
<body>
<?php
if (isset($_POST['foo']))
	{
		error_reporting(E_ALL);
		$ar = $_POST['foo'];
		var_dump($ar).'<br/>';
		$ser=stripslashes($ar);
		var_dump($ser).'<br/>';
		$unser = unserialize($ser);
		var_dump($unser).'<br/>';
	}
?>
<script language="javascript" type="text/javascript">
<!--
function clicca() {
	document.adminForm.foo.value = serialize(document.adminForm.foo.value);
}

function serialize( mixed_value ) {
    /* your function */
}
//-->
</script>

<form action="try_serialize.php" method="post" name="adminForm">
<textarea id="foo" name="foo">
a textarea with
newline</textarea>
<br />
<button type="sumbit" onclick="clicca();">Send</button>
</form>
</body>
</html>

Gravatar
Kevin van Zonneveld
20 Apr '09 Permalink

q  @ AndreaZ: Ok if you put the PHP-serialized string inside codeblocks here, I can unserialize it with PHP as well, and then test serializing it with JS.

So if you could provide me with that data that would help me a lot. thx

Gravatar
AndreaZ
20 Apr '09 Permalink

q  @Kevin: i serialized a multidimensional array with your javascript function, then i unserialized the result inside a php file: unserialize returns false (if i serialize with php it works)

how can i send to you the test that i made? it is a little php file with inside your javascript serialize function

PS: thanks a lot for your work ;-)

Gravatar
Kevin van Zonneveld
20 Apr '09 Permalink

q  @ AndreaZ: I did some testing with the following code:

$ser = serialize("a \n b");
var_dump($ser);


.. which is executable by both PHP & JS.
Both return the exact same output:

kevin@kevin-desktop:~/workspace/plutonia-phpjs/_tools$ rhino debug.js 
string(12) "s:5:"a 
 b";"
kevin@kevin-desktop:~/workspace/plutonia-phpjs/_tools$ php debug.php
string(12) "s:5:"a 
 b";"



..so I'm wondering could it be that something else is buggy in the script you are using? If not, can you supply the full input and code that gives the wrong results?

Gravatar
AndreaZ
19 Apr '09 Permalink

q  when there's a newline character (\n) inside a serialized string, php unseriliaze returns false

i don't know why :-(

Gravatar
Kevin van Zonneveld
22 Mar '09 Permalink

q  @ Thomas: You're the first to report. Would it be possible for you to supply test-data?

Gravatar
Thomas
8 Mar '09 Permalink

q  Works fine with php 5.2.0.
But doesn't work with php 5.2.6 ! Php cannot unserialize the string.

Any known issues about this ?

Gravatar
Kevin van Zonneveld
30 Dec '08 Permalink

q  @ Garagoth: Well noticed. That doesn't make any sense at all.

Gravatar
Garagoth
18 Dec '08 Permalink

q  Hm, an interesting line of code, not sure how it is supposed to work:

[CODE=&quot;Javascript&quot;]
if (ktype == &quot;function&quot; &amp;&amp; ktype == &quot;object&quot;) {
continue;
}
[/CODE]

Cheers,
Garagoth.

Gravatar
Kevin van Zonneveld
1 Dec '08 Permalink

q  @ Andrej Pavlovic: Thanks man!

Gravatar
Tom
27 Nov '08 Permalink

q  The returned representation of provided function is valid PHP code (which is correct).
But does anyone have a JS var_export function whose returned representation is valid javascript code?
The returned value type should be string and it could be passed to eval() function.
Examples:

var a = new Array(12, '13', 'abc', 'line1\nline2\nline3');
var js_code = var_export(a);
/*
the returned value should be:
"{0:12, 1:'13', 2:'abc', 3:'line1\nline2\nline3'}"
*/

var b = {'key1':4, 'key2':'5', 'key3':'xxx\n123', 555:'text'};
js_code = var_export(b);
/*
the returned value should be:
"{'key1':4, 'key2':'5', 'key3':'xxx\n123', 555:'text'}"
*/

var c = 123;
js_code = var_export(c); // "123"

var d = '321';
js_code = var_export(d); // "'321'"

var e = 'multilne\ntext';
js_code = var_export(e); // "'multiline\ntext'"

function add(x, y)
{
      res = x + y;
      return res;
}
var js_code = var_export(add);
/*
the returned value should be:
"function add(x, y) { res = x + y; return res; }"
*/



Thanks.

Gravatar
Kevin van Zonneveld
21 Sep '08 Permalink

q  @ Dino: I've committed your changes! Thank you.

Gravatar
mopont
19 Sep '08 Permalink

q  Great function!

Gravatar
dino
19 Sep '08 Permalink

q  woops I don't think my code showed up properly.

[CODE=&quot;javascript&quot;]
for (key in mixed_value) {
var ktype = _getType(mixed_value[key]);

//alert(key + ' type is ' + ktype);
if (ktype != &quot;function&quot; &amp;&amp; ktype != &quot;object&quot;) {
okey = (key.match(/^[0-9]+$/) ? parseInt(key) : key);
vals += serialize(okey) +
serialize(mixed_value[key]);
count++;
}
}
[/CODE]

Gravatar
dino
19 Sep '08 Permalink

q  serialize doesn't work well with mootools since mootools adds or extends the array object with functions which serialize picks up on and tries to translate into a string.

At least it broke my code when I included mootools.

I fixed it by having serialize not try to translate objects or functions. It doesn't seem like functions are being handled anyway.



[CODE=&quot;javascript&quot;]
case &quot;function&quot;:
val = &quot;&quot;;
break;

for (key in mixed_value) {
var ktype = _getType(mixed_value[key]);

//alert(key + ' type is ' + ktype);
if (ktype != &quot;function&quot; &amp;&amp; ktype != &quot;object&quot;) {
okey = (key.match(/^[0-9]+$/) ? parseInt(key) : key);
vals += serialize(okey) +
serialize(mixed_value[key]);
count++;
}
}

Gravatar
Ren
9 Sep '08 Permalink

q  Sorry plz, it was my fault.
I used htmlspecialchars($_REQUEST), so the variable with serialized string encoded too.
Function works fine :) thx

Gravatar
Kevin van Zonneveld
9 Sep '08 Permalink

q  @ Ren: Can you please provide a print_r of the array in CODE blocks that you are trying to serialize? We need your import to improve this function. Thanks!

Gravatar
Thomas Buschhardt
9 Sep '08 Permalink

q  Hallo, thanx for the code. How can I find out in the returned object the length of the arrays (if these arrays be)?

Gravatar
d3x
31 May '08 Permalink

q  @ Kevin: Arpad Ray's implementation uses &quot;eval&quot; and &quot;eval is evil&quot;(http://blogs.msdn.com/ericlippert/archive/2003/11/01/53329.aspx)

Gravatar
Kevin van Zonneveld
31 May '08 Permalink

q  @ d3x: Do you think that this function beats Arpad Ray's implementation?

Gravatar
d3x
30 May '08 Permalink

q  For every other person that needs an unserialize implementation:

[CODE=&quot;Javascript&quot;]
function unserialize(data){
function error(type, msg, filename, line){throw new window[type](msg, filename, line);}
function read_until(data, offset, stopchar){
var buf = [];
var char = data.slice(offset, offset + 1);
var i = 2;
while(char != stopchar){
if((i+offset) &gt; data.length){
error('Error', 'Invalid');
}
buf.push(char);
char = data.slice(offset + (i - 1),offset + i);
i += 1;
}
return [buf.length, buf.join('')];
};
function read_chars(data, offset, length){
buf = [];
for(var i = 0;i &lt; length;i++){
var char = data.slice(offset + (i - 1),offset + i);
buf.push(char);
}
return [buf.length, buf.join('')];
};
function _unserialize(data, offset){
if(!offset) offset = 0;
var buf = [];
var dtype = (data.slice(offset, offset + 1)).toLowerCase();

var dataoffset = offset + 2;
var typeconvert = new Function('x', 'return x');
var chars = 0;
var datalength = 0;

switch(dtype){
case &quot;i&quot;:
typeconvert = new Function('x', 'return parseInt(x)');
var readData = read_until(data, dataoffset, ';');
var chars = readData[0];
var readdata = readData[1];
dataoffset += chars + 1;
break;
case &quot;b&quot;:
typeconvert = new Function('x', 'return (parseInt(x) == 1)');
var readData = read_until(data, dataoffset, ';');
var chars = readData[0];
var readdata = readData[1];
dataoffset += chars + 1;
break;
case &quot;d&quot;:
typeconvert = new Function('x', 'return parseFloat(x)');
var readData = read_until(data, dataoffset, ';');
var chars = readData[0];
var readdata = readData[1];
dataoffset += chars + 1;
break;
case &quot;n&quot;:
readdata = null;
break;
case &quot;s&quot;:
var ccount = read_until(data, dataoffset, ':');
var chars = ccount[0];
var stringlength = ccount[1];
dataoffset += chars + 2;

var readData = read_chars(data, dataoffset+1, parseInt(stringlength));
var chars = readData[0];
var readdata = readData[1];
dataoffset += chars + 2;
if(chars != parseInt(stringlength) &amp;&amp; chars != readdata.length){
error('SyntaxError', 'String length mismatch');
}
break;
case &quot;a&quot;:
var readdata = {};

var keyandchars = read_until(data, dataoffset, ':');
var chars = keyandchars[0];
var keys = keyandchars[1];
dataoffset += chars + 2;

for(var i = 0;i &lt; parseInt(keys);i++){
var kprops = _unserialize(data, dataoffset);
var kchars = kprops[1];
var key = kprops[2];
dataoffset += kchars;

var vprops = _unserialize(data, dataoffset);
var vchars = vprops[1];
var value = vprops[2];
dataoffset += vchars;

readdata[key] = value;
}

dataoffset += 1;
break;
default:
error('SyntaxError', 'Unknown / Unhandled data type(s): ' + dtype);
break;
}
return [dtype, dataoffset - offset, typeconvert(readdata)];
};
return _unserialize(data, 0)[2];
}
[/CODE]

Code translated from: http://hurring.com/scott/code/python/serialize/

Gravatar
Kevin van Zonneveld
2 Mar '08 Permalink

q  @ Andrea Giammarchi: Impressive code Andrea! I will look into this and if I use (parts of) it, I will credit you accordingly! Thanks

Gravatar
Andrea Giammarchi
2 Mar '08 Permalink

q  two years ago, 15.000 users, about zero problems:
http://www.devpro.it/javascript_id_102.html

It's able to save correctly UTF-8 strings as well.

Cheers

Gravatar
Kevin van Zonneveld
2 Mar '08 Permalink

q  @ Doug: About unserialize, just now I found a very good javascript unserialize function by Arpad Ray. I've included the function in this project. If Arpad doesn't approve however (I've sent him an email), we will still have to write it ourselves.

Gravatar
Kevin van Zonneveld
28 Feb '08 Permalink

q  @ Doug: Not yet, so feel free!

Gravatar
Doug
28 Feb '08 Permalink

q  Have you started to compile a function for unserialize yet?

Gravatar
Franck Chionna
20 Feb '08 Permalink

q  hello,

i d like to serialize a window object by a js var that contain window.open , thus to keep in memory the window open if the php page is refreshed. i tried to use your code but it says js error &quot;too much recursion... any suggestion ? thanks and congratulation for the work done

Gravatar
Ates Goral
23 Jan '08 Permalink

q  I'll take a look at why serialize() is looping.

Gravatar
Kevin van Zonneveld
23 Jan '08 Permalink

q  @ Ates Goral: Example 14 is giving: too much recursion after implementing the new get_class function in serialize

Gravatar
Kevin van Zonneveld
23 Jan '08 Permalink

q  @ Ates Goral: Works like a charm, I will build this in serialize and add it as a dependency.

About your php-strict/javascript-flexible question. I think we should stay with PHP as close as possible. Hopefully this will provide consistency &amp; clarity for end users. And interoperability between php-js-function throughout the project. This approach should also ensure that no extra function documentation has to be written because PHP's function manual will (in most cases) be valid.

Gravatar
Ates Goral
22 Jan '08 Permalink

q  Here's get_class(). I think serialize() now can re-use this one instead of the local getObjectClass() implementation.

I've added the extra instanceof checks solely to match PHP behaviour. They can be removed since JavaScript has no problem with getting class names for simple types or arrays/functions etc. This brings up the question: Are we trying to mimic PHP behaviour as closely as possible or is it all right to introduce additional functionality brought forth by the flexibility of JavaScript?

[CODE=&quot;Javascript&quot;]
function get_class(obj) {
// * example 1: get_class(new (function MyClass() {}));
// * returns 1: &quot;MyClass&quot;
// * example 2: get_class({});
// * returns 2: &quot;Object&quot;
// * example 3: get_class([]);
// * returns 3: false
// * example 4: get_class(42);
// * returns 4: false
// * example 5: get_class(window);
// * returns 5: false
// * example 6: get_class(function MyFunction() {});
// * returns 6: false

if (obj instanceof Object &amp;&amp; !(obj instanceof Array) &amp;&amp;
!(obj instanceof Function) &amp;&amp; obj.constructor) {
var arr = obj.constructor.toString().match(/function\s*(\w+)/);

if (arr &amp;&amp; arr.length == 2) {
return arr[1];
}
}

return false;
}
[/CODE]

Gravatar
Kevin van Zonneveld
22 Jan '08 Permalink

q  @ Ates Goral: Thanks, I've updated the function.

Gravatar
Kevin van Zonneveld
21 Jan '08 Permalink

q  @ uestla: Yes it probably should :) Fixed, thank you!


Contribute a New function

More functions

In this category

doubleval
empty
floatval
get_defined_vars
get_resource_type
gettype
import_request_variables
intval
is_array
is_binary
is_bool
is_buffer
is_callable
is_double
is_float
is_int
is_integer
is_long
is_null
is_numeric
is_object
is_real
is_resource
is_scalar
is_string
is_unicode
isset
print_r
» serialize
settype
strval
unserialize
var_dump
var_export

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: @HalfWinter, Paulo Freitas, Andros Peña Romo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !