Copy link to clipboard
Copied
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...
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
...Copy link to clipboard
Copied
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>
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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>
Copy link to clipboard
Copied
Yes indeed . I already edit my reply and changed the status of your reply to correct, it works as a charme.
I also needed to add a "target="_blank" to the existing code of flickr, that was just a simple replace (on a piece of code that ofcourse will always be the same):
<cfset url = REReplacenocase(str,'"><img','" target="_blank"><img',"all")>
Gonna dive into the regex some more, very useful!
Thanks!