UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

1,026 lines (790 loc) 39.7 kB
<!-- Generated by Rakefile:build --> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2012-07-10 16:31:35 <br /> @Anum: You mean it is giving you the string &quot;[object Object]&quot; as output, or you are expecting an object? This should be converting a properly serialized string into an object. If you are getting the string, do you have sample code? <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2012-07-10 16:28:50 <br /> @Jaroslaw Czarniak: I have fixed this in Git. Hopefully we can keep this function (and all others) friendly to UTF8 which everyone should be using now anyways. <hr /> <strong> Jaroslaw Czarniak </strong> on 2012-06-07 18:48:32 <br /> Unserialize doesn't work (again) with german umlauts (ö, ä, ü, ß) and other not latin characters. for example folowing code: <pre><code> var test = { a: &quot;Martin&quot;, b: &quot;Schmidt&quot;, c: &quot;äöüß&quot;, d: &quot;&amp;&amp;%%$$&quot; }; var x = serialize(test); var test2 = unserialize(x); </code></pre> return: <pre><code> a =&gt; &quot;Martin&quot; b =&gt; &quot;Schmidt&quot; c =&gt; &quot;䶼߀&quot; d =&gt; &quot;&amp;&amp;%%$$&quot; </code></pre> is there any way to fix it ? <hr /> <strong> Anum </strong> on 2012-05-01 06:47:07 <br /> It gave me [object Object] as an output, i dont understand whats happening here. <hr /> <strong> <a href="http://www.webonedivision.net" rel="nofollow">Diana</a> </strong> on 2012-04-10 06:18:43 <br /> Great stuff, both serialize and unserialize work great for arrays! Thank you very much. <hr /> <strong> </strong> on 2011-06-20 15:19:45 <br /> Hi, I dont understand why is this decoding from utf8. I was getting wierd characters, so I just commented the uft8_decode function. It's cool now (I use utf8 only or whenever possible) <hr /> <strong> Dj </strong> on 2011-06-01 15:09:10 <br /> Adding the following code to the switch statement will allow you to unserialize php objects to javascript array. So you can take a string that was serialized by the php serialize() function and unserialize it with the javascript function. <pre><code> case 'o': readdata = {}; ccount = read_until(data, dataoffset, ':'); chrs = ccount[0]; stringlength = ccount[1]; dataoffset += chrs + 2; readData = read_chrs(data, dataoffset + 1, parseInt(stringlength, 10)); chrs = readData[0]; var className = readData[1]; dataoffset += chrs + 2; if (chrs == parseInt(stringlength, 10) || chrs == className.length) { keyandchrs = read_until(data, dataoffset, ':'); chrs = keyandchrs[0]; keys = keyandchrs[1]; dataoffset += chrs + 2; for (var i = 0; i &lt; parseInt(keys, 10); i++){ var kprops = _unserialize(data, dataoffset); var kchrs = kprops[1]; var key = kprops[2]; dataoffset += kchrs; var vprops = _unserialize(data, dataoffset); var vchrs = vprops[1]; var value = vprops[2]; dataoffset += vchrs; //Ignore non public properties (first char of key is null) if (typeof value !== 'undefined' &amp;&amp; typeof key === 'string' &amp;&amp; key.charCodeAt(0) ) { readdata[key] = value; } } dataoffset += 1; } break; </code></pre> <hr /> <strong> <a href="http://twitter.com/ramonlechuga" rel="nofollow">ramonlechuga</a> </strong> on 2011-04-17 20:42:35 <br /> Great! Is so nice to find this , an already made function of unserialize and serialize :D you save my time thank you so much. <hr /> <strong> mansur muzafarov </strong> on 2010-10-27 12:27:59 <br /> it doesn't work with arrays http://www.phpguru.org/downloads/PHP_Unserialize/PHP_Unserialize.js here you can find working example <hr /> <strong> </strong> on 2010-05-04 23:48:11 <br /> @mscdex: I don't think PHP sessions can be unserialized in PHP itself. The format a PHP session is stored in (the one you provided) differs from the format serialize($_SESSION) would return, which is what you need to use. However, after looking at the code above for the unserialize function, I don't see a case statement for &quot;O,&quot; so you're right that this function won't work with objects. <hr /> <strong> mscdex </strong> on 2010-05-04 23:25:09 <br /> I'm having the same problem as Alind (this function doesn't seem to support PHP objects). I am attempting to unserialize a PHP session, but the session string starts with: <pre><code> breadCrumbs|O:15:&quot;BreadCrumbStack&quot;:3:{s:22:&quot; BreadCrumbStack stack&quot;;a:2:{i:0;a:6:{s:7:&quot;item_id&quot;; ...... </code></pre> What happens as a result of unserializing this string is that the function sees the first 'b' in 'breadCrumbs' and thinks it found a boolean value, which causes unserialize() to return a value of true in this case. Is there a (already existing) fix for this? <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2010-02-19 12:12:51 <br /> @plotas: It looks as though you are trying to use it within an XPCOM component. Is that right? If so, as with also JavaScript code modules (see https://developer.mozilla.org/en/JavaScript_code_modules ), certain JavaScript environments do not automatically include a window object. So, you can either pass in a window object to the component at some point to initialize the window used by the component (esp. if you need a particular window, though that is not important in this case), or you may be able to simply use this to define &quot;window&quot;. It is possible that just using &quot;window&quot; could cause errors (I believe I've tried to redefine &quot;alert&quot; in that context and couldn't use it either), in such case, just rename &quot;window&quot;, but hopefully you won't have to do that. If your code that is using unserialize() is on an object/class you'll need to ensure &quot;window&quot; is defined as a property of the object; otherwise, it can be defined globally within the component. <pre><code>this.window = Components.classes['@mozilla.org/appshell/appShellService;1'].getService(Components.interfaces.nsIAppShellService).hiddenDOMWindow;</code></pre> I believe there is another way to get a window in such code too, but the above works for me. Note that in regular client-side JS, &quot;this.window&quot; will translate to &quot;window.window&quot; which conveniently for us, simply refers back to the window object. Same effect I believe in XPCOM and code modules. Depending on your needs, if you weren't aware, Firefox also includes the JSON (see https://developer.mozilla.org/en/JSON , though maybe you need nsIJSON for XPCOM code: https://developer.mozilla.org/en/nsIJSON ) for object serialization. <hr /> <strong> <a href="http://rokdd.de/s/scripts/" rel="nofollow">plotas</a> </strong> on 2010-02-19 09:28:28 <br /> Hi your function looks great but it does not work for me in FF 3.5 .. i get always the error:<pre><code>Error: that.window[type] is not a constructor Source File: file:///E:/Dienste/FirefoxPortable/Data/profile/extensions/%7Be4a8a97b-f2ed-450b-b12d-ee082ba24781%7D/components/greasemonkey.js Line: 1305</code></pre> well maybe that is because try to use in greasemonkey? any idea? Thaanks! <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-11-07 13:54:23 <br /> @Lyle Pratt: By altering the error message to return the offset, it seems that the function is running into problems where you have a space at the end of a string (i.e., after &quot;107 West Denison Drive&quot;) if that helps you or someone else to debug this. Not sure if the space is being dropped somewhere or something... <hr /> <strong> <a href="baylor.edu" rel="nofollow">Lyle Pratt</a> </strong> on 2009-11-04 09:40:10 <br /> Hello, I am having some serious problems using this script in all versions of IE. The error is getting thrown at line 146 (from the code excerpt on this page). It seems that that switch statement is getting defaulted a few times and its throwing off the whole unserialization. Can someone take a look at this? Im in a pretty big bind at the moment. Here is a sample of what I am trying to unserialize that is failing (works in Chrome, safari, and FF): <pre><code> a:18:{s:10:&quot;fa_essay_1&quot;;a:4:{s:7:&quot;storage&quot;;s:16:&quot;Essay Question 1&quot;;s:5:&quot;value&quot;;s:8:&quot;testfest&quot;;s:2:&quot;id&quot;;s:10:&quot;fa_essay_1&quot;;s:5:&quot;group&quot;;s:10:&quot;fa_essay_1&quot;;}s:10:&quot;fa_essay_2&quot;;a:4:{s:7:&quot;storage&quot;;s:16:&quot;Essay Question 2&quot;;s:5:&quot;value&quot;;s:16:&quot;unset_this_value&quot;;s:2:&quot;id&quot;;s:10:&quot;fa_essay_2&quot;;s:5:&quot;group&quot;;s:10:&quot;fa_essay_2&quot;;}s:15:&quot;form_first_name&quot;;a:4:{s:7:&quot;storage&quot;;s:10:&quot;First Name&quot;;s:5:&quot;value&quot;;s:7:&quot;Bobby J&quot;;s:2:&quot;id&quot;;s:15:&quot;form_first_name&quot;;s:5:&quot;group&quot;;s:15:&quot;form_first_name&quot;;}s:16:&quot;form_middle_name&quot;;a:4:{s:7:&quot;storage&quot;;s:11:&quot;Middle Name&quot;;s:5:&quot;value&quot;;s:2:&quot;P.&quot;;s:2:&quot;id&quot;;s:16:&quot;form_middle_name&quot;;s:5:&quot;group&quot;;s:16:&quot;form_middle_name&quot;;}s:14:&quot;form_last_name&quot;;a:4:{s:7:&quot;storage&quot;;s:9:&quot;Last Name&quot;;s:5:&quot;value&quot;;s:6:&quot;Baylor&quot;;s:2:&quot;id&quot;;s:14:&quot;form_last_name&quot;;s:5:&quot;group&quot;;s:14:&quot;form_last_name&quot;;}s:19:&quot;form_preferred_name&quot;;a:4:{s:7:&quot;storage&quot;;s:14:&quot;Preferred Name&quot;;s:5:&quot;value&quot;;s:5:&quot;Bubba&quot;;s:2:&quot;id&quot;;s:19:&quot;form_preferred_name&quot;;s:5:&quot;group&quot;;s:19:&quot;form_preferred_name&quot;;}s:13:&quot;form_birthday&quot;;a:4:{s:7:&quot;storage&quot;;s:8:&quot;Birthday&quot;;s:5:&quot;value&quot;;s:9:&quot;12-JUN-62&quot;;s:2:&quot;id&quot;;s:13:&quot;form_birthday&quot;;s:5:&quot;group&quot;;s:13:&quot;form_birthday&quot;;}s:11:&quot;form_gender&quot;;a:4:{s:7:&quot;storage&quot;;s:6:&quot;Gender&quot;;s:5:&quot;value&quot;;s:1:&quot;M&quot;;s:2:&quot;id&quot;;s:11:&quot;form_gender&quot;;s:5:&quot;group&quot;;s:11:&quot;form_gender&quot;;}s:12:&quot;form_country&quot;;a:4:{s:7:&quot;storage&quot;;s:7:&quot;Country&quot;;s:5:&quot;value&quot;;s:13:&quot;United States&quot;;s:2:&quot;id&quot;;s:12:&quot;form_country&quot;;s:5:&quot;group&quot;;s:12:&quot;form_country&quot;;}s:12:&quot;form_address&quot;;a:4:{s:7:&quot;storage&quot;;s:7:&quot;Address&quot;;s:5:&quot;value&quot;;s:24:&quot;107 West Denison Drive &quot;;s:2:&quot;id&quot;;s:12:&quot;form_address&quot;;s:5:&quot;group&quot;;s:12:&quot;form_address&quot;;}s:10:&quot;form_phone&quot;;a:4:{s:7:&quot;storage&quot;;s:12:&quot;Phone Number&quot;;s:5:&quot;value&quot;;s:16:&quot;unset_this_value&quot;;s:2:&quot;id&quot;;s:10:&quot;form_phone&quot;;s:5:&quot;group&quot;;s:10:&quot;form_phone&quot;;}s:10:&quot;form_email&quot;;a:4:{s:7:&quot;storage&quot;;s:5:&quot;Email&quot;;s:5:&quot;value&quot;;s:34:&quot;Bobby_Baylor-University@baylor.edu&quot;;s:2:&quot;id&quot;;s:10:&quot;form_email&quot;;s:5:&quot;group&quot;;s:10:&quot;form_email&quot;;}s:10:&quot;form_major&quot;;a:4:{s:7:&quot;storage&quot;;s:5:&quot;Major&quot;;s:5:&quot;value&quot;;s:10:&quot;Accounting&quot;;s:2:&quot;id&quot;;s:10:&quot;form_major&quot;;s:5:&quot;group&quot;;s:10:&quot;form_major&quot;;}s:19:&quot;form_classification&quot;;a:4:{s:7:&quot;storage&quot;;s:14:&quot;Classification&quot;;s:5:&quot;value&quot;;s:8:&quot;Freshman&quot;;s:2:&quot;id&quot;;s:19:&quot;form_classification&quot;;s:5:&quot;group&quot;;s:19:&quot;form_classification&quot;;}s:16:&quot;fa_art_interests&quot;;a:4:{s:7:&quot;storage&quot;;s:13:&quot;Art Interests&quot;;s:5:&quot;value&quot;;s:8:&quot;testfest&quot;;s:2:&quot;id&quot;;s:16:&quot;fa_art_interests&quot;;s:5:&quot;group&quot;;s:16:&quot;fa_art_interests&quot;;}s:17:&quot;fa_film_interests&quot;;a:4:{s:7:&quot;storage&quot;;s:32:&quot;Film and Digital Media Interests&quot;;s:5:&quot;value&quot;;s:16:&quot;unset_this_value&quot;;s:2:&quot;id&quot;;s:17:&quot;fa_film_interests&quot;;s:5:&quot;group&quot;;s:17:&quot;fa_film_interests&quot;;}s:18:&quot;fa_music_interests&quot;;a:4:{s:7:&quot;storage&quot;;s:15:&quot;Music Interests&quot;;s:5:&quot;value&quot;;s:16:&quot;unset_this_value&quot;;s:2:&quot;id&quot;;s:18:&quot;fa_music_interests&quot;;s:5:&quot;group&quot;;s:18:&quot;fa_music_interests&quot;;}s:20:&quot;fa_theatre_interests&quot;;a:4:{s:7:&quot;storage&quot;;s:22:&quot;Theatre Arts Interests&quot;;s:5:&quot;value&quot;;s:16:&quot;unset_this_value&quot;;s:2:&quot;id&quot;;s:20:&quot;fa_theatre_interests&quot;;s:5:&quot;group&quot;;s:20:&quot;fa_theatre_interests&quot;;}} </code></pre> <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-11-04 03:39:57 <br /> @kilops, our pleasure. Kevin set up a good pattern of crediting people I think, and indeed pointing out bugs should get credit since it is the first step in fixing them! Also thanks for the positive feedback and for keeping your eye out for any other bugs... <hr /> <strong> kilops </strong> on 2009-11-03 19:02:24 <br /> Thanx &quot;Brett Zamir&quot; for the explanation. And thx again for crediting me the fix despite I only came with the bug and a bad workaround. If I see something else like that i'll keep you in touch. ps: hurray for PHP.JS and everyone making it. <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-11-03 09:25:28 <br /> @kilops: Fixed at http://github.com/kvz/phpjs/commit/ce785027aeff2a1461ff54d5fd44d35b2f2d68d7 . When we made function calls use 'this', I may have overlooked a few nested function cases where we have to declare &quot;that&quot; (or something) outside of the nested functions and then refer to &quot;that&quot; inside the nested functions (since &quot;this&quot; has a different meaning depending on the function it is in and how it is used). In this case (no pun intended), since &quot;this&quot; in a non-instantiated function refers to the global window object, and since you were using the namespaced version, it wasn't finding the method in the global namespace (as it shouldn't unless you redeclared it there). Using the &quot;that&quot; convention lets us drop through the &quot;that&quot; variable to the inner function so it can have access to the &quot;this&quot; on the enclosing method. Hope that makes sense. If not, see http://www.crockford.com/javascript/private.html <hr /> <strong> kilops </strong> on 2009-11-03 07:15:12 <br /> This function is giving me some troubles, throwing error : &quot;Function utf8_decode() does not exists --- Line ... 120&quot; Original code : <pre><code> case 's': (...) // Length was calculated on an utf-8 encoded string // so wait with decoding readdata = this.utf8_decode(readdata); break; case 'a': (...) </code></pre> I changed to : <pre><code> case 's': (...) // Length was calculated on an utf-8 encoded string // so wait with decoding readdata = PHP_JS.prototype.utf8_decode(readdata); break; case 'a': (...) </code></pre> I don't really know why but using &quot;PHP_JS.prototype.&quot; instead of &quot;this.&quot; fix my problem (despite the fact it's supposed to be the same object as far as I know) FYI : I'm using the namespaced version on FF 3.5.3 and the serialize function may have the same problem. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-10-08 22:59:22 <br /> @ Frank: Thanks for voicing your opinion : ) We're no designers. How do you like the new comment-code colors? <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-10-08 22:07:50 <br /> @ Le Torbi: Wow big kuddos to you! I must say I would have never thought of that. Thanks a lot Le Torbi! We will include this in the official repo. It will be online shortly. Thanks again! <hr /> <strong> Frank </strong> on 2009-10-08 18:05:58 <br /> This black background is horrible and makes it a pain to read this site! <hr /> <strong> <a href="http://www.letorbi.de" rel="nofollow">Le Torbi</a> </strong> on 2009-09-26 19:02:12 <br /> Whoops, I guess my bug is another than the one discussed below my post. The problem is (or was) that strings, which contain special chars (like German umlauts or Japanese), have some additional chars after they have been unserialized. For example 's:6:&quot;höhö&quot;:s:3:&quot;abc&quot;' became 'höhö&quot;:' and 'abc' (&quot;: was added to the string with the umlauts). However, the code I've postetd below fixes this issue. PS: ö is one character (German umlaut oe) not two (just in case it'll be screwed up when posted). <hr /> <strong> <a href="http://www.letorbi.de" rel="nofollow">Le Torbi</a> </strong> on 2009-09-26 17:47:14 <br /> Hi guys, I just fixed the utf8 issue. The problem was, that the given size represents the length in bytes, but it was interpreted as if it is the length in characters. I fixed it by calculating the size in bytes of each character and reducing the length accordingly. Here is the code: <pre><code> var read_chrs = function (data, offset, length){ var buf; buf = []; for (var i = 0;i &lt; length;i++){ var chr = data.slice(offset + (i - 1),offset + i); buf.push(chr); length -= utf8Overhead(chr); // NEW LINE } return [buf.length, buf.join('')]; }; var utf8Overhead = function(char) { // NEW FUNCTION var code = char.charCodeAt(0); if (code &lt; 0x0080) return 0; if (code &lt; 0x0800) return 1; return 2; }; </code></pre> I've also written a faster method to calculate the string length in bytes for the serialize() function. See my posting there for details. BTW: What do I have to to to get this into the official code? Bai Le Torbi <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-09-04 18:31:31 <br /> @ Martin: Good point. The base64 encoding function have utf8 dependencies too, so we might as well add them to the serialize functions. Added to our new git rep: http://github.com/kvz/phpjs/commit/80ef918e43ffe015c241cb31d35b3afafada7d33 <hr /> <strong> <a href="www.erlenwiese.de" rel="nofollow">Martin</a> </strong> on 2009-08-24 08:53:27 <br /> In opposite to the php unserialize function this one cannot handle utf8 strings. If you call utf8_encode() before you call unserialize() and run the returned data through utf8_decode(), it works fine. But to have the same functionality as the php function, this should be part of the unserialize function. <hr /> <strong> <a href="www.erlenwiese.de" rel="nofollow">Martin</a> </strong> on 2009-08-23 21:58:50 <br /> This does not work with german umlauts (ö, ä, ü, ß). I get the following message: Unknown / Unhandled data type(s): I'm wondering, why there is a call of &quot;encodeURIComponent&quot; in the serialize function but no &quot;decodeURIComponent&quot; in the unserialize function. Maybe it has something to do with this!? <pre><code> var test = { a: &quot;Martin&quot;, b: &quot;Schmidt&quot;, c: &quot;äöüß&quot;, d: &quot;&amp;&amp;%%$$&quot; }; var x = serialize(test); var test2 = unserialize(x); </code></pre> <hr /> <strong> fer </strong> on 2009-07-16 05:01:17 <br /> thanks does not work well with accents such as: á <hr /> <strong> <a href="kdenotes.blogspot.com" rel="nofollow">k_de surya</a> </strong> on 2009-07-07 03:45:44 <br /> thanks for the code... <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-07-03 13:48:27 <br /> @ Chris + James: Thanks for your fixes guys, I've added them to SVN <hr /> <strong> James </strong> on 2009-06-29 01:10:16 <br /> Had a problem using this on FF 3.5b4. The browser was having trouble at determining what data was so I had to make the following change: <pre><code> return _unserialize(data, 0)[2]; </code></pre> to <pre><code> return _unserialize(String(data), 0)[2]; </code></pre> <hr /> <strong> Chris </strong> on 2009-06-26 17:53:19 <br /> Regarding this code: <pre><code> case 'b': typeconvert = function (x) {return parseInt(x, 10) == 1;};</code></pre> I don't know that this could ever matter, but it should technically be `return parseInt(x, 10) != 0` because all non-zero values are true. <hr /> <strong> Brett Zamir </strong> on 2009-04-03 19:25:15 <br /> Hi Thomas, In PHP.JS, we return objects often where arrays are used in PHP. This is because JavaScript has no other way to represent associative-arrays--arrays in which the indexes are either not all numerical, or are not numbered sequentially from 0 and/or by 1.. Here's the link to count() as Kevin pointed out: http://phpjs.org/functions/view/count/ which does counting of objects too... <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-04-03 16:52:29 <br /> Why not use count :) <hr /> <strong> Thomas Buschhardt </strong> on 2009-04-02 16:41:01 <br /> Hallo, thanx for the code. How can I read the length of the list you returned in Example 1? Is this an array? I try <pre><code> var testArray=unserialize(someStuff); alert(testArray.length); </code></pre> with no luck Bye Thomas <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-12-17 14:35:48 <br /> @ Alind: It would really help if you could supply the exact code that fails so we can take a look at it, fix it, and add it as testcase to it stays fixed in the future as well. Would that be possible? <hr /> <strong> Alind </strong> on 2008-12-17 06:54:32 <br /> Unserialize function is not working for Javascript Objects can u tell me something related with this..... <hr /> <strong> <a href="http://www.exploretheearth.info" rel="nofollow">Kate</a> </strong> on 2008-09-17 17:35:17 <br /> Thank you so much for allowing me the opportunity to visit this site. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-09-13 15:45:58 <br /> @ Jakub: Thanks for contributing to our project. When I implement your change and test it, I get the following error from rhino: <pre><code> kevin@trinity:~/workspace/plutonia-phpjs/_tools$ ./phpjstest.php unserialize Exception in thread &amp;quot;Thread-0&amp;quot; org.mozilla.javascript.JavaScriptException: [object Error] (/home/kevin/workspace/plutonia-phpjs/functions/var/unserialize.js#16) at org.mozilla.javascript.gen.c4._c2(Unknown Source) at org.mozilla.javascript.gen.c4.call(Unknown Source) at org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:97) at org.mozilla.javascript.gen.c4._c5(Unknown Source) at org.mozilla.javascript.gen.c4.call(Unknown Source) at org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:97) at org.mozilla.javascript.gen.c4._c5(Unknown Source) at org.mozilla.javascript.gen.c4.call(Unknown Source) at org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:97) at org.mozilla.javascript.gen.c4._c1(Unknown Source) at org.mozilla.javascript.gen.c4.call(Unknown Source) at org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:97) at org.mozilla.javascript.gen.c1._c1(Unknown Source) at org.mozilla.javascript.gen.c1.call(Unknown Source) at org.mozilla.javascript.ScriptRuntime.applyOrCall(ScriptRuntime.java:2234) at org.mozilla.javascript.BaseFunction.execIdCall(BaseFunction.java:257) at org.mozilla.javascript.IdFunctionObject.call(IdFunctionObject.java:127) at org.mozilla.javascript.optimizer.OptRuntime.call2(OptRuntime.java:76) at org.mozilla.javascript.gen.c2._c17(Unknown Source) at org.mozilla.javascript.gen.c2.call(Unknown Source) at org.mozilla.javascript.optimizer.OptRuntime.call1(OptRuntime.java:66) at org.mozilla.javascript.gen.c2._c4(Unknown Source) at org.mozilla.javascript.gen.c2.call(Unknown Source) at org.mozilla.javascript.optimizer.OptRuntime.callProp0(OptRuntime.java:119) at org.mozilla.javascript.gen.c2._c107(Unknown Source) at org.mozilla.javascript.gen.c2.call(Unknown Source) at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:393) at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2834) at org.mozilla.javascript.gen.c2.call(Unknown Source) at org.mozilla.javascript.Context.call(Context.java:548) at org.mozilla.javascript.JavaAdapter.callMethod(JavaAdapter.java:507) at adapter1.run(&amp;lt;adapter&amp;gt;) at java.lang.Thread.run(Thread.java:619) </code></pre> <hr /> <strong> Jakub </strong> on 2008-09-11 15:50:16 <br /> Hi, I have noticed that for some strings, this 'for' should be executed 'length -1' not 'length' times: <pre><code> for(var i = 0;i &amp;lt; length;i++){ var chr = data.slice(offset + (i - 1),offset + i); buf.push(chr); } </code></pre> when I have changed it to: <pre><code> for(var i = 0;i &amp;lt; length-1;i++){ var chr = data.slice(offset + (i - 1),offset + i); buf.push(chr); } </code></pre> it has worked well. BTW. You are doing a great job here. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-08-27 16:32:56 <br /> @ nimbus: This has been caused by a logic-mistake. JavaScript distincts associative arrays from objects. PHP does not. I've made some adjustments to correct the behaviour &amp;amp; increase compatibility with PHP. And for as testing the functions, I've just finished writing a CLI script that can now test functions from the commandline, so expect the quality to be higher in the near future. Of course, input (ideas for tests, improvements) by readers is very much appreciated. <hr /> <strong> nimbus </strong> on 2008-07-28 15:17:47 <br /> serializing arrays has two major flaws: a) non-numeric indexes cause the function to break! b) negative values break your function! did you test your code at all?! please fix or at least tell people - it took me quite a while to find that out! :-( <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-07-22 20:02:33 <br /> @ dptr1988: I've committed your fix and credited you accordingly. Thank you very much dptr1988!! <hr /> <strong> Bug when key names have spaces </strong> on 2008-07-22 18:18:26 <br /> I have really enjoyed using these php.js functions. They sure make it easy for a PHP developer to use JavaScript I have been using unserialize() on Firefox 3.0 and noticed that if the key name contains spaces they are not unserialized correctly. Down near the end of the function, right before it eval()'s the code, replace this code with the following: Buggy code: <pre><code> for (key in objprops[0]) { objout += &amp;quot;&amp;quot; + key + &amp;quot;=objprops[0]['&amp;quot; + key + &amp;quot;'];&amp;quot;; }</code></pre> Fixed code: <pre><code> for (key in objprops[0]) { objout += &amp;quot;this['&amp;quot; + key + &amp;quot;']=objprops[0]['&amp;quot; + key + &amp;quot;'];&amp;quot;; }</code></pre> dptr1988 <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-07-17 22:16:09 <br /> @ Lukasz: Can you post me the serializedObject so we can test it? <hr /> <strong> Lukasz </strong> on 2008-06-27 08:11:47 <br /> You have race conditions in your code for the unserialize. That is you have return statement on multiple lines which sometimes but not always causes the an error Example: ###################### var deserializedArray = unserialize(serializedObject); var x = deserializedArray['someFiled']; ###################### Sometimes if I want to use the field ['someField'] of the deserializedArray immediately on the next line of code after calling the function unserialize I get the error saying that deserializedArray['someFiled'] has no properites. This is caused beacuse YOU HAVE RETUNED FROM THE FUNCTION BEFORE YOU HAVE FINISHED DESERIALIZING ALL THE FIELDS, AND THE CODE CONTINUES. I guess it is the way you used recursion that is the problem. The actual output from Firebug: ###################### unserialize() finished :: unserialize() finished :: unserialize() finished :: unserialize() finished :: unserialize() finished :: unserialize() finished :: unserialize() finished :: unserialize() finished :: unserialize() finished :: unserialize() finished :: unserialize() finished :: unserialize() finished :: unserialize() finished :: unserialize() finished :: unserialize() finished :: unserialize() finished :: unserialize() finished :: unserialize() finished :: unserialize() finished :: unserialize() finished :: unserialize() finished :: unserialize() finished :: unserialize() finished :: unserializeResp has no properties [Break on this error] var anonsArr = unserializeResp['anons_media'] ###################### <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-04-28 09:03:20 <br /> @ alberto: Of course there are many ways to transport data between server- and clientside. Which way is better depends on your situation. In this project however it's all about mimicking PHP functionality. And serialize &amp;amp; unserialize are two functions that do just that, and I wouldn't be surprised if this is faster then generating &amp;amp; parsing XML. Which is a pretty heavy format actually. Best in most cases would probably be JSON though. Because since PHP5, PHP can <pre><code> $json = json_encode($array); </code></pre> And JavaScript can interpret JSON natively: <pre><code> for ( item in json ) { } </code></pre> JSON will also produce the smallest output compared to XML &amp;amp; serialized arrays. For safe transportation you may want to additionally: <pre><code> base = base64_encode(json_encode($data)); </code></pre> <pre><code> data = base64_decode(base); </code></pre> base64 functions have also been ported in this project. <hr /> <strong> alberto </strong> on 2008-04-27 15:22:03 <br /> wouldn't it be better just to transform the php object data to an XML object that is easy to hang by Javascript? Anybody interested in this way email me albertomelchor@hotmail.com <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-04-03 08:49:21 <br /> @ Devin: Logically you're right but my guess is that Arpad Ray had performance on his mind when writing it like that. It's a recursive function so every bit of performance tweaking will help. I think the first check may be less CPU intensive than the second one. Though the second one probably needs to be in place for arrays and other cases, it is skipped if the first condition is true, thus saving you a calculation. If you have other thoughts on that please let me know. <hr /> <strong> Devin </strong> on 2008-04-03 05:02:21 <br /> You have: <pre><code> if (inp == &amp;quot;&amp;quot; || inp.length &amp;lt; 2) { </code></pre> Isn't the string length of &amp;quot;&amp;quot; &amp;lt; 2 anyway? How about: <pre><code> if (inp.length &amp;lt; 2) { </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-03-20 15:13:16 <br /> @ Pedro Tainha: Awesome! Thanks for the kind words and improved code! <hr /> <strong> Pedro Tainha </strong> on 2008-03-20 06:52:43 <br /> Hello!!! I was searching for this function in javascript a long time ago and happy to finally fond it. But when I tryed didn´t work with me. I sent an email for the author but he didn´t reply (probably he had no time - no problem), so i had to learn the code and find the problems. I´m glad because i solved my problem and at the same time, because i never tryed to contribute with something, this was inspiring and i can see that probably anyone can do it as well - Don´t be shy, try it!!! (please don´t look at the language errors, I´m from Portugal and I don´t write with much frequency) Now let´s get with the real stuff, that in reality isn´t so much. the lines that were changed and comented with: '//changed...' 1. In the array case, the code was only prepared for the number of fields less then 10, now suports any number of fields 2. In the boolean case, the semicolon afther the &amp;quot;0&amp;quot; or &amp;quot;1&amp;quot; boolean variables wasn´t cuted. Thanks very much to Arpad Ray, you did a great job. This function is very useful for me!!! And will be for others, i will try to put a great toturial about this function and php using AJAX. I prefer this instead Json to receive data from php. The code is above: <pre><code> function unserialize ( inp ) { // http://kevin.vanzonneveld.net // + original by: Arpad Ray (mailto:arpad@php.net) // * example 1: unserialize(); // * returns 1: ['Kevin', 'van', 'Zonneveld'] // //******************************************************************************* // // * Reviewed at: 19/03/2008 // + by: Pedro Tainha (email@pedrotainha.com or pedrotainha@gmail.com) // // Notes: // Fixed bugs for the cases: 'b'(boolean) and 'a'(array) // //******************************************************************************* error = 0; if (inp == &amp;quot;&amp;quot; || inp.length &amp;lt; 2) { errormsg = &amp;quot;input is too short&amp;quot;; return; } var val, kret, vret, cval; var type = inp.charAt(0); var cont = inp.substring(2); var size = 0, divpos = 0, endcont = 0, rest = &amp;quot;&amp;quot;, next = &amp;quot;&amp;quot;; switch (type) { case &amp;quot;N&amp;quot;: // null if (inp.charAt(1) != &amp;quot;;&amp;quot;) { errormsg = &amp;quot;missing ; for null&amp;quot;; } // leave val undefined rest = cont; break; case &amp;quot;b&amp;quot;: // boolean if (!/[01];/.test(cont.substring(0,2))) { errormsg = &amp;quot;value not 0 or 1, or missing ; for boolean&amp;quot;; } val = (cont.charAt(0) == &amp;quot;1&amp;quot;); rest = cont.substring(2); //changed... break; case &amp;quot;s&amp;quot;: // string val = &amp;quot;&amp;quot;; divpos = cont.indexOf(&amp;quot;:&amp;quot;); if (divpos == -1) { errormsg = &amp;quot;missing : for string&amp;quot;; break; } size = parseInt(cont.substring(0, divpos)); if (size == 0) { if (cont.length - divpos &amp;lt; 4) { errormsg = &amp;quot;string is too short&amp;quot;; break; } rest = cont.substring(divpos + 4); break; } if ((cont.length - divpos - size) &amp;lt; 4) { errormsg = &amp;quot;string is too short&amp;quot;; break; } if (cont.substring(divpos + 2 + size, divpos + 4 + size) != &amp;quot;\&amp;quot;;&amp;quot;) { errormsg = &amp;quot;string is too long, or missing \&amp;quot;;&amp;quot;; } val = cont.substring(divpos + 2, divpos + 2 + size); rest = cont.substring(divpos + 4 + size); break; case &amp;quot;i&amp;quot;: // integer case &amp;quot;d&amp;quot;: // float var dotfound = 0; for (var t = 0; t &amp;lt; cont.length; t++) { cval = cont.charAt(t); if (isNaN(parseInt(cval)) &amp;amp;&amp;amp; !(type == &amp;quot;d&amp;quot; &amp;amp;&amp;amp; cval == &amp;quot;.&amp;quot; &amp;amp;&amp;amp; !dotfound++)) { endcont = t; break; } } if (!endcont || cont.charAt(endcont) != &amp;quot;;&amp;quot;) { errormsg = &amp;quot;missing or invalid value, or missing ; for int/float&amp;quot;; } val = cont.substring(0, endcont); val = (type == &amp;quot;i&amp;quot; ? parseInt(val) : parseFloat(val)); rest = cont.substring(endcont + 1); break; case &amp;quot;a&amp;quot;: // array if (cont.length &amp;lt; 4) { errormsg = &amp;quot;array is too short&amp;quot;; return; } divpos = cont.indexOf(&amp;quot;:&amp;quot;, 1); if (divpos == -1) { errormsg = &amp;quot;missing : for array&amp;quot;; return; } size = parseInt(cont.substring(1*divpos, 0)); //changed... cont = cont.substring(divpos + 2); val = new Array(); if (cont.length &amp;lt; 1) { errormsg = &amp;quot;array is too short&amp;quot;; return; } for (var i = 0; i + 1 &amp;lt; size * 2; i += 2) { kret = unserialize(cont, 1); if (error || kret[0] == undefined || kret[1] == &amp;quot;&amp;quot;) { errormsg = &amp;quot;missing or invalid key, or missing value for array&amp;quot;; return; } vret = unserialize(kret[1], 1); if (error) { errormsg = &amp;quot;invalid value for array&amp;quot;; return; } val[kret[0]] = vret[0]; cont = vret[1]; } if (cont.charAt(0) != &amp;quot;}&amp;quot;) { errormsg = &amp;quot;missing ending }, or too many values for array&amp;quot;; return; } rest = cont.substring(1); break; case &amp;quot;O&amp;quot;: // object divpos = cont.indexOf(&amp;quot;:&amp;quot;); if (divpos == -1) { errormsg = &amp;quot;missing : for object&amp;quot;; return; } size = parseInt(cont.substring(0, divpos)); var objname = cont.substring(divpos + 2, divpos + 2 + size); if (cont.substring(divpos + 2 + size, divpos + 4 + size) != &amp;quot;\&amp;quot;:&amp;quot;) { errormsg = &amp;quot;object name is too long, or missing \&amp;quot;:&amp;quot;; return; } var objprops = unserialize(&amp;quot;a:&amp;quot; + cont.substring(divpos + 4 + size), 1); if (error) { errormsg = &amp;quot;invalid object properties&amp;quot;; return; } rest = objprops[1]; var objout = &amp;quot;function &amp;quot; + objname + &amp;quot;(){&amp;quot;; for (key in objprops[0]) { objout += &amp;quot;&amp;quot; + key + &amp;quot;=objprops[0]['&amp;quot; + key + &amp;quot;'];&amp;quot;; } objout += &amp;quot;}val=new &amp;quot; + objname + &amp;quot;();&amp;quot;; eval(objout); break; default: errormsg = &amp;quot;invalid input type&amp;quot;; } return (arguments.length == 1 ? val : [val, rest]); } </code></pre> Pedro Tainha - email@pedrotainha.com www.pedrotainha.com (in construction) <hr />