Actionscript:
-
function decode(str:String):Object {
-
var vars:Object={};
-
var parse:Array =str.split("&");
-
var i:int=0;
-
for (i = 0; i<parse.length; i++) {
-
var pair:Array=parse[i].split("=");
-
if (pair.length==2) {
-
vars[pair[0]]=pair[1];
-
}
-
}
-
return vars;
-
}
-
-
var nameValuePairs="one=1&&two=éllo&three=1000&four=0xFF0000";
-
var parsed:Object=decode(nameValuePairs);
-
-
trace(parsed.one);
-
trace(parsed.two);
-
trace(parsed.three);
-
trace(parsed.four);
-
-
/*outputs:
-
1
-
éllo
-
1000
-
0xFF0000
-
*/
Well not exactly a URLVariables replacement, but I often use the URLVariables.decode() function and find that its very picky... like if there is an extra & or something it freaks out and breaks, or if there are line returns. As a quick and simple solution the other day I wrote this function into part of a class (Model class) that I was working on. Fixed the problem and even handles Spanish characters nicely.
There is plenty of room for improvement with this simple function... feel free to post improvements in the comments.
2 Comments
Some time ago i wrote replacement for URLVariables to add support of objects and arrays. As you may know, PHP can accept arrays from URL:
?array[0]=1&array[1]=2&array=3
Here is posting about this class:
http://translate.google.com/translate?hl=ru&sl=ru&tl=en&u=http%3A%2F%2Factualwave.com%2Fblog%2F%3Fp%3D91
very nice a_[w]
One Trackback
[...] URLVariables Replacement [...]