11Feb/101
Code Snippet: Extract A Value From A Querystring in AS3
I found myself in need of extracting a given value from a query string that was inside an XML attribute. Searching for something like this, I couldn't find anything on par with PHP's parse_str() so I got the string out of the XML and wrote this handy function:
function getQueryStringVar(queryStr:String, targetVar:String):String
{
var pairs = queryStr.split("&");
for(var i:String in pairs)
{
var splitStr = pairs[i].split("=");
if(splitStr[0] == targetVar)return splitStr[1];
}
return "";
}
Usage is easy. Just send it your query string as the first parameter (make sure there's not a "?" at the beginning) and key you want to know the value of.
// myVal is set to "example"
var myVal:String = getQueryStringVar("val1=test&val2=example", "val2");
Let me know if you find this function useful or if you improve upon it.



February 12th, 2010 - 19:58
For sake of efficiency, type all your variables:
var pairs:Array
var splitStr:Array