I got a little excited about my last post and using Nu in apache so I wrote a simple function to parse the query string. You know, that bit of ?key=value that shows up on some websites. In PHP, you access that using $_GET as an associative array so I wrote the following function:
(function GET ()
(set qs (((NSProcessInfo processInfo) environment) objectForKey:"QUERY_STRING"))
(set lines (qs componentsSeparatedByString:"&"))
(set dict (NSMutableDictionary dictionary))
(lines each: (do (line)
(set parts (line componentsSeparatedByString:"="))
(if (> (parts count) 1)
(dict setObject:((parts objectAtIndex:1) stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding) forKey:((parts objectAtIndex:0) stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding))
)
))
(dict)
)
This way, you can use ((GET) objectForKey:"key"). You may prefer to set it to a local variable so that it doesn’t keep getting calculated and you can avoid some parentheses ((set get (GET)) (get valueForKey:"key")).
Leave a Reply