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

Replacing text in a string

Participant ,
Jan 17, 2008 Jan 17, 2008

Copy link to clipboard

Copied

Hello. I am pulling in a friend's rss feed and we are trying to figure out how to make images display correctly. Here is a sample chunk that I pull in:

I am posting a forum image here Image: http://mysite/test.jpg

How would I be able to make the "Image: http://mysite/test.jpg" part if that post change to "<img src=" http://mysite/test.jpg">"

I tried REReplace but couldn't get it to replace correctly. Can someone please help me out?
TOPICS
Advanced techniques

Views

226

Translate

Translate

Report

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
Guest
Jan 17, 2008 Jan 17, 2008

Copy link to clipboard

Copied

<cfset str = "test test test Image: http://mysite/test.jpg test test test" />
<cfset str = rereplaceNoCase(str, 'Image: (https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)', '<img src="\1" />', 'all') />

<cfoutput>#htmlEditFormat(str)#</cfoutput>

Votes

Translate

Translate

Report

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 ,
Jan 17, 2008 Jan 17, 2008

Copy link to clipboard

Copied

That is awesome. If you want to, or have time, I'd love to understand how that works.

Votes

Translate

Translate

Report

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
Guest
Jan 17, 2008 Jan 17, 2008

Copy link to clipboard

Copied

LATEST
well, i'm no regex guru... that pattern to match a URL was found via google 🙂

the rest of it is pretty straightforward. find any instance of "Image: " followed by the URL pattern. The URL pattern is enclosed in parentheses which means we can reference it later as \1. so you now have that URL in what's essentially a variable... pop that variable into the <img src= /> and that's all she wrote.

Votes

Translate

Translate

Report

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
Documentation