Skip to content

Instantly share code, notes, and snippets.

@keverw
Created June 14, 2012 22:56
Show Gist options
  • Save keverw/2933513 to your computer and use it in GitHub Desktop.
Save keverw/2933513 to your computer and use it in GitHub Desktop.
http query string parse
list convertHttpQ(string str) //converts strided list to query string
{
list strided_output = [];
//remove ?
string first_letter = llGetSubString(str, 0, 0);
if (first_letter == "?")
{
str = llGetSubString(str, 1, llStringLength(str));
}
//get all the & parts
list urlparts = llParseString2List(str,["&"],[]);
//loop them
integer listLength = llGetListLength(urlparts);
integer x;
for (x = 0; x < listLength; x++)
{
string urlpart = llList2String(urlparts, x);
if(llSubStringIndex(urlpart, "=") != -1) //has a equal
{
list parts = llParseString2List(urlpart,["="],[]);
strided_output += [llList2String(parts, 0), llList2String(parts, 1)];
}
else //no equal
{
strided_output += [urlpart, ""];
}
}
return strided_output;
}
string $_GET(list array, string variable)
{
integer len = llGetListLength(array);
integer i;
for(i = 0; i < len; i = i + 2)
{
if (llList2String(array, i) == variable)
{
return llList2String(array, i + 1);
}
}
return "";
}
string $_POST(list array, string variable)
{
return $_GET(array, variable);
}
string print_r(list array)
{
string output = "Array";
output += "(";
integer len = llGetListLength(array);
integer i;
for(i = 0; i < len; i = i + 2)
{
//\t - tab
output += "\n" + "\t" + "[" + llList2String(array, i) + "] => [" + llList2String(array, i + 1) + "]";
}
output += "\n" + ")";
return output;
}
default
{
state_entry()
{
string var = "?Name=Syn&last=&age=19&loc";
list httpvars = convertHttpQ(var);
llSay(0, $_GET(httpvars, "age"));
//llSay(0, print_r(httpvars));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment