Solved

How can I keep <sup>whatever</sup> & <sub>whatever</sub> html tags in my coldfusion string but get rid of all other html tags?

Posted on 2016-08-06
5
40 Views
Last Modified: 2016-08-07
How can I keep <sup>whatever</sup> & <sub>whatever</sub> html tags in my coldfusion string but get rid of all other html tags?
0
Comment
Question by:brihol44
  • 3
  • 2
5 Comments
 
LVL 25

Accepted Solution

by:
dgrafx earned 500 total points
ID: 41746371
Right before you strip the tags replace the sup tags with some character that doesn't exist in the string like a [ and a ] so it looks like
[sup] and [/sup].
Then strip the tags.
Then replace the [sup] and [/sup] 
with <sup> and </sup>.

Open in new window

0
 

Author Comment

by:brihol44
ID: 41746482
Nice! Have it working but is there a way to condense this code wise? Is there a delimiter to do something like... <cfset editedValue = REReplace(editedValue, "<sup>|</sup>", "|", "ALL")> (only for conveying to you) to condense the code? Or is this just handled like "ReReplace(REReplace(...." just curious if there's a pre-defined delimiter.

Thank you!

<cfset editedValue = "<sup>Hello!</sup>" >
<cfset editedValue = REReplace(editedValue, "<sup>", "[sup]", "ALL")>
<cfset editedValue = REReplace(editedValue, "</sup>", "[/sup]", "ALL")>
<cfset editedValue = REReplace(editedValue, "<sub>", "[sub]", "ALL")>
<cfset editedValue = REReplace(editedValue, "</sub>", "[/sub]", "ALL")>
<cfset editedValue = REReplace(editedValue, "(<[^>]*>)", "", "ALL")>
<cfset editedValue = Replace(editedValue, "[sup]", "<sup>", "ALL")>
<cfset editedValue = Replace(editedValue, "[/sup]", "</sup>", "ALL")>
<cfset editedValue = Replace(editedValue, "[sub]", "<sub>", "ALL")>
<cfset editedValue = Replace(editedValue, "[/sub]", "</sub>", "ALL")>

Open in new window

0
 

Author Closing Comment

by:brihol44
ID: 41746485
Thank you!
0
 

Author Comment

by:brihol44
ID: 41746487
oops. my example was not wrapped by code tags...
<cfset editedValue = REReplace(editedValue, "<sup>|</sup>", "[sup]|[/sup]", "ALL")>

Open in new window

0
 
LVL 25

Expert Comment

by:dgrafx
ID: 41746589
Here ya go!

<b>Original:</b><br />
<cfsavecontent variable="editedValue">
<b>This</b> is some <sub>testing</sub> <u>text</u>. <sup>Hello</sup>!<br>
I walk <sub>that</sub> a <b>child</b> <sup>might</sup> <i>live</i> ...
</cfsavecontent>
#variables.editedValue#<br />
<hr>

<b>Preserve sub & sup:</b><br>
<cfset editedValue = ReplaceNoCase(ReplaceNoCase(ReplaceNoCase(ReplaceNoCase(variables.editedValue,
		"</sub>","[/sub]","ALL"),"<sub>","[sub]","ALL"),"</sup>","[/sup]","ALL"),"<sup>","[sup]","ALL")
	>
#variables.editedValue#<br />
<hr>

<b>Strip tags:</b><br>
<cfset editedValue = ReReplace(variables.editedValue, "(,)|(<[^>]*>)", "", "All")>
#variables.editedValue#<br>
<hr>

<b>Final:</b><br>
<cfset editedValue = ReplaceNoCase(ReplaceNoCase(ReplaceNoCase(ReplaceNoCase(variables.editedValue,
		"[/sub]","</sub>","ALL"),"[sub]","<sub>","ALL"),"[/sup]","</sup>","ALL"),"[sup]","<sup>","ALL")
	>
#variables.editedValue#

Open in new window

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now