<cfcomponent>
<cffunction name=”tagStripper” access=”public” output=”no” returntype=”string”>
<cfargument name=”source” required=”YES” type=”string”>
<cfargument name=”action” required=”No” type=”string” default=”strip”>
<cfargument name=”tagList” required=”no” type=”string” default=”">
<!—
source = string variable
This is the string to be modified
action = “preserve” or “strip”
This function will either strip all tags except
those specified in the tagList argument, or it will
preserve all tags except those in the taglist argument.
The default action is “strip”
tagList = string variable
This argument contains a comma separated list of tags to be excluded from
the action. If the action is “strip”, then these tags won’t be stripped.
If the action os “preserve”, then these tags won’t be preserved (ie, only
these tags will be stripped)
EXAMPLE
tagStripper(myString,”strip”,”b,i”)
This invocation will strip all html tags except for
<b></b> and <i></i>
—>
<cfscript>
var str = arguments.source;
var i = 1;
var tag = “”;
if (trim(lcase(action)) eq “preserve”)
{
// strip only the exclusions
for (i=1;i lte listlen(arguments.tagList); i = i + 1)
{
tag = listGetAt(tagList,i);
str = REReplaceNoCase(str,”</?#tag#.*?>”,”",”ALL”);
}
} else {
// if there are exclusions, mark them with NOSTRIP
if (tagList neq “”)
{
for (i=1;i lte listlen(tagList); i = i + 1)
{
tag = listGetAt(tagList,i);
str = REReplaceNoCase(str,”<(/?#tag#.*?)>”,”___TEMP___NOSTRIP___\1___TEMP___ENDNOSTRIP___”,”ALL”);
}
}
str = reReplaceNoCase(str,”</?[A-Z].*?>”,”",”ALL”);
// convert excluded tags back to normal
str = replace(str,”___TEMP___NOSTRIP___”,”<”,”ALL”);
str = replace(str,”___TEMP___ENDNOSTRIP___”,”>”,”ALL”);
}
return str;
</cfscript>
</cffunction>
<!— From Ben Nadel: http://www.bennadel.com/index.cfm?event=blog.viewcode&id=1155&index=1 —>
<cffunction
name=”CleanHighAscii”
access=”public”
returntype=”string”
output=”false”
hint=”Cleans extended ascii values to make the as web safe as possible.”>
<!— Define arguments. —>
<cfargument
name=”Text”
type=”string”
required=”true”
hint=”The string that we are going to be cleaning.”
/>
<!— Set up local scope. —>
<cfset var LOCAL = {} />
<!—
When cleaning the string, there are going to be ascii
values that we want to target, but there are also going
to be high ascii values that we don’t expect. Therefore,
we have to create a pattern that simply matches all non
low-ASCII characters. This will find all characters that
are NOT in the first 127 ascii values. To do this, we
are using the 2-digit hex encoding of values.
—>
<cfset LOCAL.Pattern = CreateObject(
“java”,
“java.util.regex.Pattern”
).Compile(
JavaCast( “string”, “[^\x00-\x7F]” )
)
/>
<!—
Create the pattern matcher for our target text. The
matcher will be able to loop through all the high
ascii values found in the target string.
—>
<cfset LOCAL.Matcher = LOCAL.Pattern.Matcher(
JavaCast( “string”, ARGUMENTS.Text )
) />
<!—
As we clean the string, we are going to need to build
a results string buffer into which the Matcher will
be able to store the clean values.
—>
<cfset LOCAL.Buffer = CreateObject(
“java”,
“java.lang.StringBuffer”
).Init() />
<!— Keep looping over high ascii values. —>
<cfloop condition=”LOCAL.Matcher.Find()”>
<!— Get the matched high ascii value. —>
<cfset LOCAL.Value = LOCAL.Matcher.Group() />
<!— Get the ascii value of our character. —>
<cfset LOCAL.AsciiValue = Asc( LOCAL.Value ) />
<!—
Now that we have the high ascii value, we need to
figure out what to do with it. There are explicit
tests we can perform for our replacements. However,
if we don’t have a match, we need a default
strategy and that will be to just store it as an
escaped value.
—>
<!— Check for Microsoft double smart quotes. —>
<cfif (
(LOCAL.AsciiValue EQ 8220) OR
(LOCAL.AsciiValue EQ 8221)
)>
<!— Use standard quote. —>
<cfset LOCAL.Value = “”"” />
<!— Check for Microsoft single smart quotes. —>
<cfelseif (
(LOCAL.AsciiValue EQ 8216) OR
(LOCAL.AsciiValue EQ 8217)
)>
<!— Use standard quote. —>
<cfset LOCAL.Value = “‘” />
<!— Check for Microsoft elipse. —>
<cfelseif (LOCAL.AsciiValue EQ 8230)>
<!— Use several periods. —>
<cfset LOCAL.Value = “…” />
<cfelse>
<!—
We didn’t get any explicit matches on our
character, so just store the escaped value.
—>
<cfset LOCAL.Value = “&###LOCAL.AsciiValue#;” />
</cfif>
<!—
Add the cleaned high ascii character into the
results buffer. Since we know we will only be
working with extended values, we know that we don’t
have to worry about escaping any special characters
in our target string.
—>
<cfset LOCAL.Matcher.AppendReplacement(
LOCAL.Buffer,
JavaCast( “string”, LOCAL.Value )
) />
</cfloop>
<!—
At this point there are no further high ascii values
in the string. Add the rest of the target text to the
results buffer.
—>
<cfset LOCAL.Matcher.AppendTail(
LOCAL.Buffer
) />
<!— Return the resultant string. —>
<cfreturn LOCAL.Buffer.ToString() />
</cffunction>
<!— From http://www.trunkful.com/index.cfm/2010/5/27/How-to-CFMAIL-Properly-and-Keep-the-SPAM-in-the-Can —>
<cffunction name=”textMessage” access=”public” returntype=”string” hint=”Converts an html email message into a nicely formatted with line breaks plain text message”>
<cfargument name=”string” required=”true” type=”string”>
<cfscript>
var pattern = “<br>”;
var CRLF = chr(13) & chr(10);
var message = ReplaceNoCase(arguments.string, pattern, CRLF , “ALL”);
pattern = “<[^>]*>”;
</cfscript>
<cfreturn REReplaceNoCase(message, pattern, “” , “ALL”)>
</cffunction>
<!— Removes all html entities (i.e. Ε ) from a string —>
<cffunction name=”cleanHTMLEntities” access=”public” returntype=”string” hint=”Removes all html entities from a string”>
<cfargument name=”input” required=”true” type=”string”>
<cfset output = REReplaceNoCase(input, “(&##\d{3};)|(&[a-zA-Z1-4]+;)|(&##x[A-F0-9]+;)”, “”, “all”)>
<cfreturn output>
</cffunction>
<cfscript>
/**
* Coverts special characters to character entities, making a string safe for display in HTML.
* Version 2 update by Eli Dickinson (eli.dickinson@gmail.com)
* Fixes issue of lists not being equal and adding bull
* v3, extra semicolons
*
*9/14/10 It was called HTMLSafe, but I needed to get the ASCII from the html entities
* for the subject line of cf email since cf wasn’t converting the entities. I also added a
* bunch of upper & lower greek letters and removed some code
* that dealt with different versions of cf since I only have the one and it’s a newer one.
*
* @param string String to format. (Required)
* @return Returns a string.
* @author Gyrus (eli.dickinson@gmail.comgyrus@norlonto.net)
* @version 3, August 30, 2006
*/
function ReverseHTMLSafe(string) {
// Initialise
var goodChars = “&,”",#Chr(161)#,#Chr(162)#,#Chr(163)#,#Chr(164)#,#Chr(165)#,#Chr(166)#,#Chr(167)#,#Chr(168)#,#Chr(169)#,#Chr(170)#,#Chr(171)#,#Chr(172)#,#Chr(173)#,#Chr(174)#,#Chr(175)#,#Chr(176)#,#Chr(177)#,#Chr(178)#,#Chr(179)#,#Chr(180)#,#Chr(181)#,#Chr(182)#,#Chr(183)#,#Chr(184)#,#Chr(185)#,#Chr(186)#,#Chr(187)#,#Chr(188)#,#Chr(189)#,#Chr(190)#,#Chr(191)#,#Chr(215)#,#Chr(247)#,#Chr(192)#,#Chr(193)#,#Chr(194)#,#Chr(195)#,#Chr(196)#,#Chr(197)#,#Chr(198)#,#Chr(199)#,#Chr(200)#,#Chr(201)#,#Chr(202)#,#Chr(203)#,#Chr(204)#,#Chr(205)#,#Chr(206)#,#Chr(207)#,#Chr(208)#,#Chr(209)#,#Chr(210)#,#Chr(211)#,#Chr(212)#,#Chr(213)#,#Chr(214)#,#Chr(216)#,#Chr(217)#,#Chr(218)#,#Chr(219)#,#Chr(220)#,#Chr(221)#,#Chr(222)#,#Chr(223)#,#Chr(224)#,#Chr(225)#,#Chr(226)#,#Chr(227)#,#Chr(228)#,#Chr(229)#,#Chr(230)#,#Chr(231)#,#Chr(232)#,#Chr(233)#,#Chr(234)#,#Chr(235)#,#Chr(236)#,#Chr(237)#,#Chr(238)#,#Chr(239)#,#Chr(240)#,#Chr(241)#,#Chr(242)#,#Chr(243)#,#Chr(244)#,#Chr(245)#,#Chr(246)#,#Chr(248)#,#Chr(249)#,#Chr(250)#,#Chr(251)#,#Chr(252)#,#Chr(253)#,#Chr(254)#,#Chr(255)#”;
var badChars = “&,",¡,¢,£,¤,¥,¦,§,¨,©,ª,«,¬,­,®,¯,°,±,²,³,´,µ,¶,·,¸,¹,º,»,¼,½,¾,¿,×,÷,À,Á,Â,Ã,Ä,Å,Æ,Ç,È,É,Ê,Ë,Ì,Í,Î,Ï,Ð,Ñ,Ò,Ó,Ô,Õ,Ö,Ø,Ù,Ú,Û,Ü,Ý,Þ,ß,à,á,â,ã,ä,å,æ,ç,è,é,ê,ë,ì,í,î,ï,ð,ñ,ò,ó,ô,õ,ö,ø,ù,ú,û,ü,ý,þ,ÿ,&##338;,&##339;,&##352;,&##353;,&##376;,&##710;,&##8211;,&##8212;,&##8216;,&##8217;,&##8218;,&##8220;,&##8221;,&##8222;,&##8224;,&##8225;,&##8240;,&##8249;,&##8250;,&##8364;,<sup><small>TM</small></sup>,•, ,Α,α,Β,β,Γ,γ,Δ,δ,Ε,ε,Ζ,ζ,Η,η,Θ,θ,Ι,ι,Κ,κ,&Lamda;,&lamda;,Μ,μ,Ν,ν,Ξ,ξ,Ο,ο,Π,π,Ρ,ρ,Σ,σ,ς,Τ,τ,Υ,υ,Φ,φ,Χ,χ,Ψ,ψ,Ω,ω”;
// MX/Unicode matches
goodChars = “#goodChars#,#Chr(338)#,#Chr(339)#,#Chr(352)#,#Chr(353)#,#Chr(376)#,#Chr(710)#,#Chr(8211)#,#Chr(8212)#,#Chr(8216)#,#Chr(8217)#,#Chr(8218)#,#Chr(8220)#,#Chr(8221)#,#Chr(8222)#,#Chr(8224)#,#Chr(8225)#,#Chr(8240)#,#Chr(8249)#,#Chr(8250)#,#Chr(8364)#,#Chr(8482)#,#Chr(8226)#,#Chr(160)#,#Chr(913)#,#Chr(945)#,#Chr(914)#,#Chr(946)#,#Chr(915)#,#Chr(947)#,#Chr(916)#,#Chr(948)#,#Chr(917)#,#Chr(949)#,#Chr(918)#,#Chr(950)#,#Chr(919)#,#Chr(951)#,#Chr(920)#,#Chr(952)#,#Chr(921)#,#Chr(953)#,#Chr(922)#,#Chr(954)#,#Chr(923)#,#Chr(955)#,#Chr(924)#,#Chr(956)#,#Chr(925)#,#Chr(957)#,#Chr(926)#,#Chr(958)#,#Chr(927)#,#Chr(959)#,#Chr(928)#,#Chr(960)#,#Chr(929)#,#Chr(961)#,#Chr(931)#,#Chr(963)#,#Chr(962)#,#Chr(932)#,#Chr(964)#,#Chr(933)#,#Chr(965)#,#Chr(934)#,#Chr(966)#,#Chr(935)#,#Chr(967)#,#Chr(936)#,#Chr(968)#,#Chr(937)#,#Chr(969)#”;
// Return immediately if blank string
if (NOT Len(Trim(string))) return string;
// Do replacing
return ReplaceList(string, badChars, goodChars);
}
</cfscript>
</cfcomponent>