Skip to main content
Inspiring
October 13, 2012
Answered

How to find/replace between <a href / a>

  • October 13, 2012
  • 1 reply
  • 1041 views

Hi all,

How do i find / replace between <a href / a> so that i can replace the link with my own?

I have to aply that to this code:

<a href="http://www.flickr.com/photos/aaronrz/6956154572/" title="Spiritual by Aaron Reiff-Zall, on Flickr"><img src="http://farm8.staticflickr.com/7050/6956154572_a9a5c3a179_m.jpg" width="240" height="160" alt="Spiritual"></a>

I have searched the net but can't find any example, or is there?

Thanks...

This topic has been closed for replies.
Correct answer BKBK

Use regular expressions. For example, the following code will replace the value of the href attribute with http://www.myDomain.com. (View the page source). You could make the regex more efficient, of course. It all depends on your skill. However, you have to remember that, in this example, the title attribute remains unchanged.

<cfset str='<a href="http://www.flickr.com/photos/aaronrz/6956154572/" title="Spiritual by Aaron Reiff-Zall, on Flickr"><img src="http://farm8.staticflickr.com/7050/6956154572_a9a5c3a179_m.jpg" width="240" height="160" alt="Spiritual"></a>'>

<cfoutput>#REReplacenocase(str,'href\s*=\s*"[^"]+"','href="http://www.myDomain.com"',"all")#</cfoutput>

1 reply

BKBK
BKBKCorrect answer
Adobe Expert
October 13, 2012

Use regular expressions. For example, the following code will replace the value of the href attribute with http://www.myDomain.com. (View the page source). You could make the regex more efficient, of course. It all depends on your skill. However, you have to remember that, in this example, the title attribute remains unchanged.

<cfset str='<a href="http://www.flickr.com/photos/aaronrz/6956154572/" title="Spiritual by Aaron Reiff-Zall, on Flickr"><img src="http://farm8.staticflickr.com/7050/6956154572_a9a5c3a179_m.jpg" width="240" height="160" alt="Spiritual"></a>'>

<cfoutput>#REReplacenocase(str,'href\s*=\s*"[^"]+"','href="http://www.myDomain.com"',"all")#</cfoutput>

Inspiring
October 13, 2012

Hi thanks for your time, to be honoust my skills with regex are zero.

I already had managed to replace a part of the url "http://www.flickr.com/photos/" with a simple replace.

But ofcourse the end of the url differs for each image, in this case "aaronrz/6956154572/".

So that's really what i am looking for, how to replace the changing last part.

Another issue is that i need to grab the image path so i can put it in a variable.

Thanks!

Ah sorry i just tried your example and it works like a charme, thanks for helping out, for more advanced regex i will have to dive into docs about it. Thanks!

BKBK
Adobe Expert
October 13, 2012

The solution I have given replaces the whole URL. So it will still work! Just add the dynamic part to the new URL. An example to illustrate:

<cfset dynamicPart = "BKBK/1234567890/">

<cfset newHref = 'href="http://www.myDomain.com/photos/' & dynamicPart & '"'>

<cfset str='<a href="http://www.flickr.com/photos/aaronrz/6956154572/" title="Spiritual by Aaron Reiff-Zall, on Flickr"><img src="http://farm8.staticflickr.com/7050/6956154572_a9a5c3a179_m.jpg" width="240" height="160" alt="Spiritual"></a>'>

<cfoutput>#REReplacenocase(str,'href\s*=\s*"[^"]+"', newHref, "all")#</cfoutput>