Skip to content

Instantly share code, notes, and snippets.

@boughtonp
Created April 30, 2014 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boughtonp/dcd50e8e654c705df6c8 to your computer and use it in GitHub Desktop.
Save boughtonp/dcd50e8e654c705df6c8 to your computer and use it in GitHub Desktop.
QueryStringToStruct
<cffunction name="QueryStringToStruct" returntype="struct" output=false access="public"
hint="Converts a querystring into a struct of name value pairs."
>
<cfargument name="QS" type="string" required=true />
<cfargument name="Decode" type="Boolean" default=true />
<cfargument name="Delimiter" type="String" default=","
hint="Used to delimit values provided from multiple keys of same name."
/>
<cfset var Results = StructNew() />
<cfloop index="local.CurItem" list=#Arguments.QS# delimiters="&" >
<cfif Arguments.Decode>
<cfset var Key = UrlDecode(ListFirst(CurItem,"=")) />
<cfset var Value = UrlDecode(ListRest(CurItem,"=")) />
<cfelse>
<cfset var Key = ListFirst(CurItem,"=") />
<cfset var Value = ListRest(CurItem,"=") />
</cfif>
<cfif StructKeyExists( Results , Key )>
<cfset Results[Key] &= Arguments.Delimiter & Value />
<cfelse>
<cfset Results[Key] = Value />
</cfif>
</cfloop>
<cfreturn Results />
</cffunction>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment