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

Stripping

Participant ,
Jan 04, 2008 Jan 04, 2008

Copy link to clipboard

Copied

Hello.

I am pulling chunks of html code and only want to display a snippet of the chunk - the image mainly.

Here is what I am currently pulling:

<p><a href=" http://www.yahoo.com">Yahoo!</a> news:</p>
<p><a href=" http://www.yahoo.com/story/2166317809/" title="Test"><img src=" http://server3.static.yahoo.com/2378/2166317809_10a0c4_m.jpg" width="240" height="160" alt="Test" /></a></p>

I want to end up with just this:

<img src=" http://server3.static.yahoo.com/2378/2166317809_10a0c4_m.jpg" width="240" height="160" alt="Test" />

I have done something along the same lines with an image path (I stripped off the characters after the second . ) but I was wondering how to do it with this longer string.

Any ideas?

Thanks!
TOPICS
Advanced techniques

Views

537

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
Valorous Hero ,
Jan 04, 2008 Jan 04, 2008

Copy link to clipboard

Copied

Try using a regular expression. Though not the best, something along these lines

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

Copy link to clipboard

Copied

Ok can you step me through that?

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
Valorous Hero ,
Jan 04, 2008 Jan 04, 2008

Copy link to clipboard

Copied

The function uses 4 parameters:
reFindNoCase( theRegularExpression, theString, startAtCharacter, returnSubExpressions)

If you break down the regular expression <img[^>]+>:

The first part finds the start of an image tag: <img
Followed by one or more characters that are not the closing tag: [^>]+
Followed by the closing tag: >

When you set returnSubExpressions to true, the result is an array containing the starting position and length of the matched string: ie result.pos[1] and result.len[1]. You can use those values, with the MID function, to obtain the matched substring.

<cfset matchedSubstring = Mid(chunkOfHTML, result.pos[1], result.len[1])>

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

Copy link to clipboard

Copied

Brilliant

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

Copy link to clipboard

Copied

How do I output the variable the has the html stored so the browser interprets it as the actual html instead of just outputting the literal value.

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
Valorous Hero ,
Jan 05, 2008 Jan 05, 2008

Copy link to clipboard

Copied

Just remove the <pre> tags and HTMLEditFormat and it should behave as html.

<cfoutput>#newString#</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 05, 2008 Jan 05, 2008

Copy link to clipboard

Copied

Yep that's it. Thanks again!

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

How about this one?

I am pulling in a friend's forum rss feed and we are trying to figure out how to make images display correctly. When people post images on his forum, they don't post correctly when I pull the feed. 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" update to <img src=" http://mysite/test.jpg">

TIA

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
Valorous Hero ,
Jan 20, 2008 Jan 20, 2008

Copy link to clipboard

Copied

Sorry I did not respond. The forums appeared to be down. Did you get this sorted out yet?

(Btw, you have PM disabled)

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

Copy link to clipboard

Copied

I finally just buckled down and read some tutorials about it. I am getting a decent grasp on it, but still have some trouble translating some of the more advanced regex code. I will enable PM.

Cheers!

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
Engaged ,
Jan 20, 2008 Jan 20, 2008

Copy link to clipboard

Copied

LATEST
As an aside, to test you regex and refine it I would like to recommend Regex Coach http://www.weitz.de/regex-coach/ it is really super helpful!

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