Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Participant ,
Oct 12, 2012 Oct 12, 2012

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...

964
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 13, 2012 Oct 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

...
Translate
Community Expert ,
Oct 13, 2012 Oct 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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 13, 2012 Oct 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 13, 2012 Oct 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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 13, 2012 Oct 13, 2012
LATEST

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources