Solved

How do I split a complex URL string in Coldfusion?

Posted on 2010-09-20
3
1,283 Views
Last Modified: 2012-06-27
Hello,

I have a URL string with multiple query parameters.  What I would like to do is use a <cfscript function to split the string based on each variable and then build an array with it that will be returned.

In other words, say I have the following URL string:

www.example.com/test.cfm?test1=foo&test2=foo2&test3=foo3

What I woudl like to do is pass that string into a function and then output the variables in an array.

Example of outputted array: ( I know this is not what it looks like when you dump it, but I'm sure you know what I mean.)

test1 | foo
test2 | foo2
test3 | foo3

Thank you very much in advance!!!!
0
Comment
Question by:stracqan
3 Comments
 
LVL 15

Accepted Solution

by:
danrosenthal earned 500 total points
ID: 33721561
Should be something like this...
<CFSET qs = "www.example.com/test.cfm?test1=foo&test2=foo2&test3=foo3">
<CFSET qsparams = LISTLAST(qs,"?")>

<CFSET myarray = arraynew(2)>
<CFLOOP FROM="1" TO="#listlen(qsparams,"&")#" index="i">
   <CFSET thisparam = LISTGETAT(qsparams,i,"&")>
   <CFSET myarray[i][1] = LISTFIRST(thisparam,"=")>
   <CFSET myarray[i][2] = LISTLAST(thisparam,"=")>
</CFLOOP>

Open in new window

0
 
LVL 52

Expert Comment

by:_agx_
ID: 33721595
(no points ...)

>> LISTFIRST(thisparam,"=")

Just watch out for empty values.  CF list functions tend to ignore those.  If you think you might have empty values like:  something=(empty value)

     www.example.com/test.cfm?test1=foo&something=

Then you might want to use ListToArray(string, delim, preserveEmptyElements)  instead.
0
 

Author Closing Comment

by:stracqan
ID: 33721612
Worked Perfectly!!! Thanks!!!!!!!!!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question