Solved

How do I split a complex URL string in Coldfusion?

Posted on 2010-09-20
3
1,242 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now