using some technique to fix the url

100 views Asked by At

i am getting a url from cfhttp page request as:

<a href="http://subdomian.maindomain.com/javascript:popOne('http://www.rs.com ')">http://www.rs.com</a>

i am trying to replace it as:

<a href="http://www.rs.com" target="_blank">http://www.rs.com</a>

the rs.cfm is gonna be dynamic everytime, it can be sb,sd,ge etc etc

and i want to add a target to the a href

trying it like this

<cfset lnk = replace(cfhttp.filecontent,'subdomian.maindomain.com/http://www.al.com/','http://www.al.com/','ALL')>

this works in one case where the name is al, but do not add the target attribute to it. also

2

There are 2 answers

0
Miguel-F On

Something like the following should work. NOTE: I have not tested this code. I will leave that up to you.

Your beginning string is:

http://subdomain.maindomain.com/javascript:popOne('http://www.rs.com ')

You can remove the beginning part by doing something like the following:

<cfset lnk = "http://subdomain.maindomain.com/javascript:popOne('http://www.rs.com ')">
<cfset lnk = ReplaceNoCase(lnk,"http://subdomain.maindomain.com/javascript:popOne('")>

This should leave lnk set to the following:

http://www.rs.com ')

Now you need to remove the trailing part like so:

<cfset lnk = ReplaceNoCase(lnk," ')")>  <!--- remove the space as well --->

Which should leave you with the URL that you want:

http://www.rs.com

Now just create your HTML output as desired:

<cfoutput>
<a href="#lnk#" target="_blank">#lnk#</a>
</cfoutput>

This sample code assumes that the strings like subdomain.maindomain.com ... do not change. If the string is dynamic then this code will not work and will need to be modified to possibly use a regex instead.

Putting it all together:

<cfset lnk = "http://subdomain.maindomain.com/javascript:popOne('http://www.rs.com ')">
<cfset lnk = ReplaceNoCase(lnk,"http://subdomain.maindomain.com/javascript:popOne('")>
<cfset lnk = ReplaceNoCase(lnk," ')")>  <!--- remove the space as well --->

<cfoutput>
<a href="#lnk#" target="_blank">#lnk#</a>
</cfoutput>

Hope that helps.

0
Deepak Jose On

Please try this function :

 <cffunction name="convertLink">
    <cfargument name="siteLink" required="true" type="string">
    <cfset extractedLink = listGetAt(replace(replace(arguments.siteLink, "('", "||", "all"), "')", "||", "all"), 2, "||")>
    <cfreturn '<a href="' & extractedLink & '" target="_blank">'& extractedLink & '</a>'>
</cffunction>