[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

URL to Variables

Posted on 2004-11-30
6
Medium Priority
?
224 Views
Last Modified: 2013-12-24
Ok...I closed my first question too soon it appear.

What I need to do is take a url for example www.mysite.com/directory1/directory2/directory3/ and making each directory a variable.   Below is the code that kind of does what I need:

<cfset url1 = "http://www.mywebsite.com/d1/d2/d3/">
<cfset dir_num = 0>


<cfloop condition = "dir_num less than 3">
     <cfset first_slash = 0>
     <cfset second_slash = 1>
     <cfset ind = 1>
     <cfloop condition="ind less than or equal to len(url1)">
          <cfif #Mid(url1, ind, 1)# eq "/">
               <cfset first_slash = #second_slash#>
               <cfset second_slash = #ind#>
          </cfif>
          <cfset ind = #ind# + 1>  
     </cfloop>
     <cfset length = #second_slash# - #first_slash# - 1>
     <cfset directory = Mid(#url1#, #first_slash#+1, #length#)>

     <cfswitch expression="#dir_num#">
       <cfcase value="0">
         <cfset directory3 = #directory#>
       </cfcase>
       <cfcase value="1">
         <cfset directory2 = #directory#>
       </cfcase>
       <cfcase value="2">
         <cfset directory1 = #directory#>
       </cfcase>
     </cfswitch>

     <cfset url1 = Mid(url1,1,len(url1)-#length#-1)>

     <cfset dir_num = #dir_num# + 1>
</cfloop>

<cfoutput>
d1 = #directory1#
d2 = #directory2#
d3 = #directory3#
</cfoutput>

I need this code to do a couple more things:
1. if there is a file specified ignore it....I just want the directories returned as variables
2. only define variables for directories defined for example if the site is www.mysite.com/directory1/directory2/  there will only be two variables defined ...if there are 3 directories...3 variables will be defined.  
3. completely ignore the host...I do not need this returned as a variable...
0
Comment
Question by:mhixon
  • 4
6 Comments
 

Expert Comment

by:ftvcs
ID: 12706896
You should see the url string as a list, separated with '/'.
If a file is specied, :

<cfset urlarray = arrayNew(1)>
<cfset c=0>
<cfloop index = "index_name"    list = "#url1#"    delimiters = "/">
<cfif not index_name contains ".">
   <cfset urlarray[c]=index_name>
   <cfset c=c+1>
</cfif>
</cfloop>

<cfdump var="#urlarray#">
0
 

Author Comment

by:mhixon
ID: 12708462
I get the following error:

The element at position 0 of array variable &quot;URLARRAY&quot; cannot be found.
0
 
LVL 17

Accepted Solution

by:
Tacobell777 earned 1000 total points
ID: 12711056
<cfset variables.url1 = "http://www.mywebsite.com/d1/d2/d3/" />
<cfset variables.dir_num = 0 />

<cfloop
      index="item"
      list="#variables.url1#"
      delimiters="/">

      <cfset variables.dir_num = variables.dir_num + 1 />
      <cfset structInsert( variables, "directory" & variables.dir_num, variables.item ) />

</cfloop>
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:mhixon
ID: 12715301
I get the following error:

Cannot insert item with key directory1.  
 This key already exists.  
0
 

Author Comment

by:mhixon
ID: 12715424
Sorry about that...my error...got it to work....Im going to work with this a little bit and make sure it does everything I need it to....Ill be back in touch thanks
0
 

Author Comment

by:mhixon
ID: 12715761
Thanks...got this to do everything I want....appreciate the help!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
What You Need to Know when Searching for a Webhost Provider
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

873 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