Parsing URL

I have a URL with variables that i need to parse,

I have the following loop to strip out the sn's,

<cfset SNs = "">
<cfloop index="i" from="1" to="3">
      <cfif IsDefined(""&i)>
            <cfset intSN = Evaluate(""&i)>
            <cfif intSN NEQ "">
                  <cfquery name="GetCSN" datasource="#request.dataSource#">
                        select * from CReg where ID = #intID#
                  <cfloop query="GetChildSN">
                        <cfset SNs = ListAppend(SNs, intSN)>

I looked at using the left, right and mid functions but I wanted to see if there is a better way to strip the names (sn#) from the variables. For example sn1=alast, this should be firstname1 =a and lastname1 =last

Who is Participating?
Pravin AsarConnect With a Mentor Principal Systems EngineerCommented:
One of way to process the querystring and convert to array

Try the following snippet

<cfset myarray=ListToArray(#CGI.QUERY_STRING#, "&")>

<cfdump var="#myarray#" expand="yes">

<cfloop index="ax" from="1" to="#ArrayLen(myarray)#">
    <cfset variable=#ListToArray(#myarray[ax]#,"=")#>
    <cfdump var="#variable#" expand="yes">

adobe116Connect With a Mentor Commented:
I'm not sure I should be responding because I don't have the exact answer, but I know where you can find it. I have Ben Forta's "Teach yourself Regular Expressions" It's a tiny book and costs hardly anything... It's a real easy read and I usually read it in one sitting.  Problem is that I never use regular expressions and over a period of a few months it seeps right out of my overloaded brain.

If I found it I could probably find what you needed and I recommend it highly...

Essentially, I think you want to replace 'everything up to '&sn1=' with a blank string, and then you want to find each instance of "&snNUMBER=CharactersUpTo(&orEOL) and replace the &SN#= with a comma.

That would create your list.

The documentation for RegEx in CF is on

and I'm sure if I dig around I could find a url stripper function that does this... but it's where you would start.  Sorry I can't help more.  But if you find yourself doing this often, invest in that book... I'm going to look for it now actually because it really annoys me that I don't know the answer to your question.  Best of luck though

usky1Author Commented:
Thanks for the book suggestion and link. I will look into both of those to see if I can figure this out. I'm going to leave this open so hopefully I will get some additional help.
Pravin AsarPrincipal Systems EngineerCommented:
Here is another apporach, which processes query_string and build a CF Query Structure.

   myquery = QueryNew("name,value");
   myarray=ListToArray("#CGI.QUERY_STRING#", "&");
   for (ax=1; ax LTE ArrayLen(myarray); ax=ax+1) {
               QuerySetCell(myquery, "name", myvar[1], ax);  
            if (ArrayLen(myvar) GT 1) {
               QuerySetCell(myquery, "value", myvar[2], ax);
            else {
               QuerySetCell(myquery, "value", "", ax);
<cfdump var="#myquery#" expand="yes">
usky1Author Commented:
pravinasar: That worked great. thanks for your help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.