<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Serializing PHP data structures for Javascript</title>
	<atom:link href="http://www.sitepoint.com/blogs/2004/04/22/serializing-php-data-structures-for-javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sitepoint.com/blogs/2004/04/22/serializing-php-data-structures-for-javascript/</link>
	<description></description>
	<pubDate>Thu, 21 Aug 2008 23:27:11 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
		<item>
		<title>By: Fordi</title>
		<link>http://www.sitepoint.com/blogs/2004/04/22/serializing-php-data-structures-for-javascript/#comment-565825</link>
		<dc:creator>Fordi</dc:creator>
		<pubDate>Sat, 22 Dec 2007 04:39:36 +0000</pubDate>
		<guid isPermaLink="false">#comment-565825</guid>
		<description>Just so's you know, since PHP doesn't use the quotes as delimiters, and in fact, holds strict to the number of bytes given (s:Num:"data";), you don't have to escape anything.

As evidence, the return value of serialize('""') is: 's:2:"""";'

Anyway, I've dealt with serialization a LOT (especially for pseudoRPC systems, I pass serialized objects to PHP (as JSON support won't be widespread until PHP5 gains dominance, and an interpreted JSON decoder is too much of a performance hit), and pass JSON object back to Javascript (as a JSON encoder is a lot less insensive than a JSON decoder).  So, I've got a nicely stable PHP serializer.  I deal with the UTF problem by converting all non-ascii to HTML Entities.  Not the best way, but since I RPC strings mainly for text for websites, it's speedier than trying to figure out how many bytes a UTF symbol occupies.

Enough talk now.  Here's the code:
&lt;code&gt;Object.toPHP = function(object) {
	var type = typeof object;
	switch (type) {
		case 'undefined':
		case 'unknown': return 'N;';
	}
	if (object === null) return 'N;';
	if (object.toPHP) return object.toPHP();
	if (Object.isElement(object)) return null;
	var ret = [];
	for (var property in object) {
		var value = Object.toPHP(object[property]);
		if (value !== undefined)
			ret.push(property.toString().toPHP() + value);
	}
	return 'a:'+ret.length+':{'+ ret.join('')+'}';
}
Date.prototype.toPHP = function() {
  return 'i:'+this.getTime()+';';
}
Object.extend(String.prototype,{
	toPHP: function () {
		var s=this.escapeUTF();
		return 's:'+s.length+':"'+s+'";';
	},
	escapeUTF: function  () {
		var charCode,ret = '';
		for (i=0; i=32))?
				this.charAt(i):
				('&#38;#x' + charCode.toString(16).toUpperCase() + ';');
		}
		return ret;
	}
});
Array.prototype.toPHP = function () {
	var ret=[];
	this.each(function (v,i) {
		ret.push(i.toPHP()+(!!v.toPHP?v.toPHP():Object.toPHP(v)));
	});
	return 'a:'+ret.length+':{'+ret.join('')+'}';
};
Number.prototype.toPHP = function () {
	return (parseInt(this)==parseFloat(this)?'i':'d')+':'+this.toString()+';';
};
Boolean.prototype.toPHP = function () {
	return 'b:'+(this?'1':'0')+';';
};
Function.prototype.toPHP = function () {
	return 'N;';
};&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Just so&#8217;s you know, since PHP doesn&#8217;t use the quotes as delimiters, and in fact, holds strict to the number of bytes given (s:Num:&#8221;data&#8221;;), you don&#8217;t have to escape anything.</p>
<p>As evidence, the return value of serialize(&#8217;&#8221;"&#8216;) is: &#8217;s:2:&#8221;"&#8221;";&#8217;</p>
<p>Anyway, I&#8217;ve dealt with serialization a LOT (especially for pseudoRPC systems, I pass serialized objects to PHP (as JSON support won&#8217;t be widespread until PHP5 gains dominance, and an interpreted JSON decoder is too much of a performance hit), and pass JSON object back to Javascript (as a JSON encoder is a lot less insensive than a JSON decoder).  So, I&#8217;ve got a nicely stable PHP serializer.  I deal with the UTF problem by converting all non-ascii to HTML Entities.  Not the best way, but since I RPC strings mainly for text for websites, it&#8217;s speedier than trying to figure out how many bytes a UTF symbol occupies.</p>
<p>Enough talk now.  Here&#8217;s the code:<br />
<code>Object.toPHP = function(object) {
	var type = typeof object;
	switch (type) {
		case 'undefined':
		case 'unknown': return 'N;';
	}
	if (object === null) return 'N;';
	if (object.toPHP) return object.toPHP();
	if (Object.isElement(object)) return null;
	var ret = [];
	for (var property in object) {
		var value = Object.toPHP(object[property]);
		if (value !== undefined)
			ret.push(property.toString().toPHP() + value);
	}
	return 'a:'+ret.length+':{'+ ret.join('')+'}';
}
Date.prototype.toPHP = function() {
  return 'i:'+this.getTime()+';';
}
Object.extend(String.prototype,{
	toPHP: function () {
		var s=this.escapeUTF();
		return 's:'+s.length+':"'+s+'";';
	},
	escapeUTF: function  () {
		var charCode,ret = '';
		for (i=0; i=32))?
				this.charAt(i):
				('&amp;#x' + charCode.toString(16).toUpperCase() + ';');
		}
		return ret;
	}
});
Array.prototype.toPHP = function () {
	var ret=[];
	this.each(function (v,i) {
		ret.push(i.toPHP()+(!!v.toPHP?v.toPHP():Object.toPHP(v)));
	});
	return 'a:'+ret.length+':{'+ret.join('')+'}';
};
Number.prototype.toPHP = function () {
	return (parseInt(this)==parseFloat(this)?'i':'d')+':'+this.toString()+';';
};
Boolean.prototype.toPHP = function () {
	return 'b:'+(this?'1':'0')+';';
};
Function.prototype.toPHP = function () {
	return 'N;';
};</code></p>]]></content:encoded>
	</item>
	<item>
		<title>By: michelangelot</title>
		<link>http://www.sitepoint.com/blogs/2004/04/22/serializing-php-data-structures-for-javascript/#comment-163647</link>
		<dc:creator>michelangelot</dc:creator>
		<pubDate>Fri, 26 Jan 2007 10:36:48 +0000</pubDate>
		<guid isPermaLink="false">#comment-163647</guid>
		<description>I have tried userialize the php serialized string with js, but the special characters of iso-8859-1 are not recognized.
Help me!!</description>
		<content:encoded><![CDATA[<p>I have tried userialize the php serialized string with js, but the special characters of iso-8859-1 are not recognized.<br />
Help me!!</p>]]></content:encoded>
	</item>
	<item>
		<title>By: mysticav</title>
		<link>http://www.sitepoint.com/blogs/2004/04/22/serializing-php-data-structures-for-javascript/#comment-33288</link>
		<dc:creator>mysticav</dc:creator>
		<pubDate>Mon, 26 Jun 2006 22:08:25 +0000</pubDate>
		<guid isPermaLink="false">#comment-33288</guid>
		<description>Please can somebody can help me with this issue ?
http://www.sitepoint.com/forums/showthread.php?p=2863921#post2863921</description>
		<content:encoded><![CDATA[<p>Please can somebody can help me with this issue ?<br />
<a href="http://www.sitepoint.com/forums/showthread.php?p=2863921#post2863921" rel="nofollow">http://www.sitepoint.com/forums/showthread.php?p=2863921#post2863921</a></p>]]></content:encoded>
	</item>
	<item>
		<title>By: andot</title>
		<link>http://www.sitepoint.com/blogs/2004/04/22/serializing-php-data-structures-for-javascript/#comment-31198</link>
		<dc:creator>andot</dc:creator>
		<pubDate>Mon, 19 Jun 2006 18:24:29 +0000</pubDate>
		<guid isPermaLink="false">#comment-31198</guid>
		<description>http://www.coolcode.cn/?p=171

Here is a best PHP serialize/unserialize implementation for javascript.

It can serialize/unserialize N,b,i,d,s,U,r,R,a,O,C.

It is included in PHPRPC: http://sourceforge.net/project/showfiles.php?group_id=163368</description>
		<content:encoded><![CDATA[<p><a href="http://www.coolcode.cn/?p=171" rel="nofollow">http://www.coolcode.cn/?p=171</a></p>
<p>Here is a best PHP serialize/unserialize implementation for javascript.</p>
<p>It can serialize/unserialize N,b,i,d,s,U,r,R,a,O,C.</p>
<p>It is included in PHPRPC: <a href="http://sourceforge.net/project/showfiles.php?group_id=163368" rel="nofollow">http://sourceforge.net/project/showfiles.php?group_id=163368</a></p>]]></content:encoded>
	</item>
	<item>
		<title>By: andr3a</title>
		<link>http://www.sitepoint.com/blogs/2004/04/22/serializing-php-data-structures-for-javascript/#comment-12693</link>
		<dc:creator>andr3a</dc:creator>
		<pubDate>Fri, 13 Jan 2006 18:38:34 +0000</pubDate>
		<guid isPermaLink="false">#comment-12693</guid>
		<description>&lt;blockquote&gt;PHP does that somehow, then why JS can not? &lt;/blockquote&gt;

because it's a strange features of PHP and PHP only ... that use something like sizeof(*string) and not strlen(*string) that should count chars and not bytes used.

Maybe on PHP6 and Unicode native support, this problem will disappear ? ... I hope so :-)</description>
		<content:encoded><![CDATA[<blockquote><p>PHP does that somehow, then why JS can not? </p></blockquote>
<p>because it&#8217;s a strange features of PHP and PHP only &#8230; that use something like sizeof(*string) and not strlen(*string) that should count chars and not bytes used.</p>
<p>Maybe on PHP6 and Unicode native support, this problem will disappear ? &#8230; I hope so :-)</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Varrah</title>
		<link>http://www.sitepoint.com/blogs/2004/04/22/serializing-php-data-structures-for-javascript/#comment-12689</link>
		<dc:creator>Varrah</dc:creator>
		<pubDate>Fri, 13 Jan 2006 10:08:21 +0000</pubDate>
		<guid isPermaLink="false">#comment-12689</guid>
		<description>&lt;blockquote&gt;I hope it’s enought for UTF8 encoded interaction, not fast as iso, but efficent, isn’t it ?
&lt;/blockquote&gt;
Well, yes
Just hoped to find even faster and optimized solution. After all - PHP does that somehow, then why JS can not? :-)</description>
		<content:encoded><![CDATA[<blockquote><p>I hope it’s enought for UTF8 encoded interaction, not fast as iso, but efficent, isn’t it ?
</p></blockquote>
<p>Well, yes<br />
Just hoped to find even faster and optimized solution. After all - PHP does that somehow, then why JS can not? :-)</p>]]></content:encoded>
	</item>
	<item>
		<title>By: andr3a</title>
		<link>http://www.sitepoint.com/blogs/2004/04/22/serializing-php-data-structures-for-javascript/#comment-12686</link>
		<dc:creator>andr3a</dc:creator>
		<pubDate>Fri, 13 Jan 2006 09:58:42 +0000</pubDate>
		<guid isPermaLink="false">#comment-12686</guid>
		<description>maybe a regexp is the fastest way to do that ... but maybe you're not thinking about big size of serialized and escaped strings !!!

Well, I've created this javascript class for ajax / php "real-time" interaction (search AJSHP Project on Google if you're interested), then bytes loaded or sent should be less and not more than an XML interaction (then faster than XML) :-)

However this version, with multibytes conversion enabled, should be usable with 100 or more serialized strings inside an array, then I hope it's enought for UTF8 encoded interaction, not fast as iso, but efficent, isn't it ?

See you :-)</description>
		<content:encoded><![CDATA[<p>maybe a regexp is the fastest way to do that &#8230; but maybe you&#8217;re not thinking about big size of serialized and escaped strings !!!</p>
<p>Well, I&#8217;ve created this javascript class for ajax / php &#8220;real-time&#8221; interaction (search AJSHP Project on Google if you&#8217;re interested), then bytes loaded or sent should be less and not more than an XML interaction (then faster than XML) :-)</p>
<p>However this version, with multibytes conversion enabled, should be usable with 100 or more serialized strings inside an array, then I hope it&#8217;s enought for UTF8 encoded interaction, not fast as iso, but efficent, isn&#8217;t it ?</p>
<p>See you :-)</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Varrah</title>
		<link>http://www.sitepoint.com/blogs/2004/04/22/serializing-php-data-structures-for-javascript/#comment-12684</link>
		<dc:creator>Varrah</dc:creator>
		<pubDate>Fri, 13 Jan 2006 09:39:32 +0000</pubDate>
		<guid isPermaLink="false">#comment-12684</guid>
		<description>Well, I was thinking about some other algorythm, may be it'll be faster (actually you've already mentioned somethnig like that in e-mail): we can make the string escaped and then count its length, divided by 3 (since it's using % and two symbols for each char), yet escaping doesn't make it correct either - Latin-1 chars are stil just chars (with no % and all) after escaping.
Then, he-he :-) One could count number of % symbols in a string, like percentNum and then calculate the length as:
utfLen = strEscaped.length - (percentNum * 2) 
Now only a fast method of counting the % symbols is needed :-)</description>
		<content:encoded><![CDATA[<p>Well, I was thinking about some other algorythm, may be it&#8217;ll be faster (actually you&#8217;ve already mentioned somethnig like that in e-mail): we can make the string escaped and then count its length, divided by 3 (since it&#8217;s using % and two symbols for each char), yet escaping doesn&#8217;t make it correct either - Latin-1 chars are stil just chars (with no % and all) after escaping.<br />
Then, he-he :-) One could count number of % symbols in a string, like percentNum and then calculate the length as:<br />
utfLen = strEscaped.length - (percentNum * 2)<br />
Now only a fast method of counting the % symbols is needed :-)</p>]]></content:encoded>
	</item>
	<item>
		<title>By: andr3a</title>
		<link>http://www.sitepoint.com/blogs/2004/04/22/serializing-php-data-structures-for-javascript/#comment-12683</link>
		<dc:creator>andr3a</dc:creator>
		<pubDate>Fri, 13 Jan 2006 09:13:09 +0000</pubDate>
		<guid isPermaLink="false">#comment-12683</guid>
		<description>Hello Varrah, the multibyte function should work correctly with this version (1.6b) but in every case utf8_encode php function converts only iso-8859-1 charset.
There are few cases (for me) where an UTF-8 header is required then i think that my object should work without this feature.
Remember that multibytes version (the same with true on contructor) is really slower because "char by char parsing" for each string is a crazy way to parse quikcly some or a lot of vars and find again string value without the correct length is a killer for client-side convertion speed (for every serialized string try to reproduce the correct string checking if re-encoded length is the same of serialized integer value ... !!!).
BTW, I hope this will be the last version of this object.

Best regards and thank you again for debug.</description>
		<content:encoded><![CDATA[<p>Hello Varrah, the multibyte function should work correctly with this version (1.6b) but in every case utf8_encode php function converts only iso-8859-1 charset.<br />
There are few cases (for me) where an UTF-8 header is required then i think that my object should work without this feature.<br />
Remember that multibytes version (the same with true on contructor) is really slower because &#8220;char by char parsing&#8221; for each string is a crazy way to parse quikcly some or a lot of vars and find again string value without the correct length is a killer for client-side convertion speed (for every serialized string try to reproduce the correct string checking if re-encoded length is the same of serialized integer value &#8230; !!!).<br />
BTW, I hope this will be the last version of this object.</p>
<p>Best regards and thank you again for debug.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Varrah</title>
		<link>http://www.sitepoint.com/blogs/2004/04/22/serializing-php-data-structures-for-javascript/#comment-12682</link>
		<dc:creator>Varrah</dc:creator>
		<pubDate>Fri, 13 Jan 2006 08:49:07 +0000</pubDate>
		<guid isPermaLink="false">#comment-12682</guid>
		<description>Made a new version of the test-scrip, with the updated version of the Andrea's script: http://neworld.spb.ru/tmp/serialize/serial2.php
Seems to be ok this time.
Thanks!</description>
		<content:encoded><![CDATA[<p>Made a new version of the test-scrip, with the updated version of the Andrea&#8217;s script: <a href="http://neworld.spb.ru/tmp/serialize/serial2.php" rel="nofollow">http://neworld.spb.ru/tmp/serialize/serial2.php</a><br />
Seems to be ok this time.<br />
Thanks!</p>]]></content:encoded>
	</item>
</channel>
</rss>
