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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stripping a url of words

Status
Not open for further replies.

countdrak

Programmer
Jun 20, 2003
358
US
I am not sure if this is possible but I hope somebody has a solution.

I have a form where the user can enter two types of URL in a textfield. Something like this

1)
OR

2)
IS there a way to check if the word ID exsists and if it does then strip out only ID=23 from the URL and if anything else is entered do not do anything.

I tried ListLast but that didnt really work.
 
Code:
<cfset oldURL = "[URL unfurl="true"]http://www.mydomain.com/mypage.cfm?ID=23">[/URL]
<cfset SearchValue = "ID">
<cfset EndSearchValue = "&">
<cfset LocationOfSearchValue = FindNoCase(oldURL,SearchValue)>
<cfset x = Find(oldURL,EndSearchValue,LocationOfSearchValue)>
<cfif x EQ 0>
  <cfset newURL = Mid(oldURL,1,LocationOfSearchValue)
<cfelse>
  <cfset newURL = Mid(oldURL,1,LocationOfSearchValue) & Mid(oldURL,x,Len(oldURL)>
</cfif>

Does this make sense?

LocationOfSearchValue may have to have 1 subtracted, I'm not sure without testing.

Kris Brixon

If you're not failing every now and again, it's a sign you're not doing anything very innovative.
- W. Allen
 
sorry I forgot to account for the fact that ID might not exist.

Code:
<cfset oldURL = "[URL unfurl="true"]http://www.mydomain.com/mypage.cfm?ID=23">[/URL]
<cfset SearchValue = "ID">
<cfset EndSearchValue = "&">
<cfset LocationOfSearchValue = FindNoCase(oldURL,SearchValue)>
<cfset x = Find(oldURL,EndSearchValue,LocationOfSearchValue)>
<cfif LocationOfSearchValue GT 0>
  <cfif x EQ 0>
    <cfset newURL = Mid(oldURL,1,LocationOfSearchValue)
  <cfelse>
    <cfset newURL = Mid(oldURL,1,LocationOfSearchValue) & Mid(oldURL,x,Len(oldURL)>
  </cfif>
<cfelse>
  <cfset newURL = oldURL>
</cfif>

However, thinking about is some more would be more appropritate to be searching for the "?" instead of the ID, that way any querystring variables will be striped. If so change the "searchvalue" to "?" and remove the stuff about "x".

Kris Brixon

If you're not failing every now and again, it's a sign you're not doing anything very innovative.
- W. Allen
 
you can try this too, its a little shorter.

Code:
<cfset myurl = "[URL unfurl="true"]http://www.mydomain.com/mypage.cfm">[/URL]
<cfif findnocase("id=", myurl)>
	<cfset myid = mid(myurl, findnocase("id=", myurl), len(myurl)-findnocase("id=", myurl)+1)>
	<cfif find("&", myID)>
		<cfset myid = left(myID,findnocase("&", myID)-1)>
		<cfset myID = listLast(myID, "=")>
	<cfelse>
		<cfset myID = listLast(myID, "=")>
	</cfif>
	<cfoutput>#myid#</cfoutput>
<cfelse>
     no id found
</cfif>

A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
-Douglas Adams (1952-2001)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top