Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it possible to take part of a variable?

Status
Not open for further replies.

maverik59

Programmer
Oct 30, 2002
67
GB
I am trying to take part of a variable, i have the code which takes the URL
<cfset heading_to = URLDecode(site)>
URLDecode(site) is always in the form /images/orgcharts/pic.jpg i just want the pic.jpg part, is there a way to manipulate this in CF. I have a mysql database and this heading_to then gets stored in a mysql db so i could take it straight from that but have had no success as subqueries are not supported.

Any Help REALLY appreciated
Thanks in advance...


 
There are many ways to &quot;take part of a variable&quot; (typically called &quot;parsing&quot;).

Many of them are outlined in the ColdFusion Reference documentation under ColdFusion Functions -> String Functions.

For your particular case, you could actually do what you want in one of two simple ways:

1) The easiest would be to just use ColdFusion's built-in function to grab the file name from a path.
Code:
<CFSET picname = GetFileFromPath(&quot;#thepath#&quot;)>
If #thepath# is set to &quot;/images/orgcharts/pic.jpg&quot;, picname would now equal &quot;pic.jpg&quot;.

2) Set a custom delimiter (&quot;/&quot;) in order to treat the path as a standard ColdFusion list.
Code:
<CFSET picname = ListLast(&quot;#thepath#&quot;,&quot;/&quot;)>
Again, if #thepath# is set to &quot;/images/orgcharts/pic.jpg&quot;, picname would now equal &quot;pic.jpg&quot;.

Number 1 is the most direct route. But I mention number 2 because it can be adapted to pull out any portion of the file you'd like...
Code:
<CFSET directoryname = ListGetAt(&quot;#thepath#&quot;,2,&quot;/&quot;)>
would pull out the second element in the list... so if #thepath# is set to &quot;/images/orgcharts/pic.jpg&quot;, directoryname would now equal &quot;orgcharts&quot;.


If you're old school, you could also accomplish what you're after using the venerable Left(), Right() or Mid() functions, the same way you would in traditional programming languages:

Code:
<CFSET reversedString = Reverse(&quot;#thepath#&quot;)>
<CFSET firstHashPos = (Find(&quot;/&quot;,reversedString) - 1)>
<CFSET reversedPicname = Left(reversedString,firstHashPos)>
<CFSET picname = Reverse(&quot;#reversedPicname#&quot;)>

but why ;-)

Hope it helps,
-Carl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top