CFLib.org – Common Function Library Project

queryStringGetVar(query_string, this_var_name)

Last updated August 02, 2005

author

Shawn Seley

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Gets the value of a variable in a query string. Useful for parsing variables from URLs outside of the current "url." scope. Note: "query string" is also referred to as "search part" within cflib.org's library.

Return Values:
Returns a string.

Example:

<cfoutput>
queryStringGetVar("?x=123", "x"):<br>
#queryStringGetVar("?x=123", "x")#<br>
<br>
queryStringGetVar("x=1&y=2&z=3", "y"):<br>
#queryStringGetVar("x=1&y=2&z=3", "y")#<br>
<br>
queryStringGetVar("http://www.cflib.org/search.cfm?searchterms=%25&sort=lastupdated&sortorder=desc", "sortorder"):<br>
#queryStringGetVar("http://www.cflib.org/search.cfm?searchterms=%25&sort=lastupdated&sortorder=desc", "sortorder")#<br>
</cfoutput>

Parameters:

Name Description Required
query_string The query string to examine. Yes
this_var_name The variable to look for. Yes

Full UDF Source:

/**
 * Gets the value of a variable in a query string.
 * 
 * @param query_string      The query string to examine. (Required)
 * @param this_var_name      The variable to look for. (Required)
 * @return Returns a string. 
 * @author Shawn Seley (shawnse@hotmail.com) 
 * @version 1, August 1, 2005 
 */
function queryStringGetVar(querystring, this_var_name){
    var re_found_struct = "";
    querystring = trim(querystring);
    
    re_found_struct = REFindNoCase("(^|[\?|&])#this_var_name#=([^\&]*)", querystring, 1, "true");
    // = re_found_struct.len & re_found_struct.pos
    
    if(arrayLen(re_found_struct.pos) gte 3) {
        if (re_found_struct.pos[3] GT 0) return urlDecode(mid(querystring, re_found_struct.pos[3], re_found_struct.len[3]));
        else return "";
    } else return "";
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson