ColdFusion UDF Library - queryString
December 16th, 2008
«« Back to ColdFusion Library
Category:
STRINGS
Description:
queryString will analyze a string of name value pairs. It will change the value of any match in the new string, and will add any unfound name matches.
Parameters:
Returns:
A new name value pair string with provided values set and/or added.
Requires:
listFindNV(), listDeleteNV()
Sample Use:
queryString("first_name=Shawn&age=29","first_name=Sean&last_name=Bailly");
Returns: first_name=Shawn&last_name=Bailly&age=29
Category:
STRINGS
Description:
queryString will analyze a string of name value pairs. It will change the value of any match in the new string, and will add any unfound name matches.
Parameters:
- str (String, required)
New name value pairs to replace or add to the query string. - query (String, Default: cgi.query_string)
The name value pair string on which to perform the operation. - redirect (Variant, Default: urlEncodedFormat(query))
If true, will add redirect=urlEncodedFormat(query) to the returned string. If false, will add nothing to the returned string. If anything else, will set the redirect value in the returned string to that value. - nvSep (String, Default: =)
The delimiter between a name and value. Example, could be set to : for a style sheet. - sep (String, Default: &)
The delimiter between a name and value pair. Example, could be set to ; for a style sheet.
Returns:
A new name value pair string with provided values set and/or added.
Requires:
listFindNV(), listDeleteNV()
Sample Use:
queryString("first_name=Shawn&age=29","first_name=Sean&last_name=Bailly");
Returns: first_name=Shawn&last_name=Bailly&age=29
queryString("color:white","background-color:black;color:blue",false,":",";");
Returns: background-color:black;color:white
Copy to Clipboard
/*#depends: listFindNV,listDeleteNV#*/
function queryString(str){
var key = server.deskey;
var query = cgi.query_string;
var redirect = "";
var addRedirect = true;
var nvSep = "=";
var sep = "&";
var loop = 0;
var name = "";
var value = "";
var pos = 0;
var tmp = "";
//get querystring
if (arrayLen(arguments) gte 2)query = arguments[2];
//control redirect
if (arrayLen(arguments) gte 3){
if (isBoolean(arguments[3]))addRedirect = arguments[3];
else redirect = urlEncodedFormat(arguments[3]);
}
//name value pair "="
if (arrayLen(arguments) gte 4)nvSep = arguments[4];
//set = "&"
if (arrayLen(arguments) gte 5)sep = arguments[5];
tmp = server.listDeleteNV(query,"redirect");
if (len(tmp))redirect = encrypt(tmp,key,"des","hex");
tmp = listLen(str,sep);
for (loop = 1; loop lte tmp; loop = IncrementValue(loop)){
name = listFirst(listGetAt(str,loop,sep),nvSep);
if (listLen(listGetAt(str,loop,sep),nvSep) gte 2)value = listLast(listGetAt(str,loop,sep),nvSep);
else value = "";
pos = server.listFindNV(query,name);
if (pos)query = listSetAt(query,pos,"#name##nvSep##value#",sep);
else query = listAppend(query,"#name##nvSep##value#",sep);
};
if (addRedirect){
pos = server.listFindNV(query,"redirect");
if (pos)query = listSetAt(query,pos,"redirect#nvSep##redirect#",sep);
else query = listAppend(query,"redirect#nvSep##redirect#",sep);
}
return query;
}
// http://www.programmers.org/index.php/coldfusion-udf-library/?ckey=73&function=queryString



