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
66 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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