mhixon
asked on
URL to Variables
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)-#leng th#-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...
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)-#leng
<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...
ASKER
I get the following error:
The element at position 0 of array variable "URLARRAY" cannot be found.
The element at position 0 of array variable "URLARRAY" cannot be found.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I get the following error:
Cannot insert item with key directory1.
This key already exists.
Cannot insert item with key directory1.
This key already exists.
ASKER
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
ASKER
Thanks...got this to do everything I want....appreciate the help!
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#">