Skip to content

Instantly share code, notes, and snippets.

@homestar9
Last active May 25, 2022 19:00
Show Gist options
  • Save homestar9/8d5bccb389bd4f8a0fc72c807fdffff7 to your computer and use it in GitHub Desktop.
Save homestar9/8d5bccb389bd4f8a0fc72c807fdffff7 to your computer and use it in GitHub Desktop.
Strip HTML tags but allow some tags
<cfscript>
/**
* stripTags
* removes all html tags from a string (e.g. <p></p>)
* Inspired by: https://stackoverflow.com/questions/38811076/how-can-i-keep-sup-sub-html-tags-in-my-coldfusion-string-but-get-rid-of-al
*
* @input: the string to process
* @reIgnore: a regex of tags to ignore (e.g. </?su[bp])
*/
function stripTags( required string input, string reIgnore="" ) {
// regex to capture all tag occurences
var stripRegEx = "<[^>]+>";
var result = createObject("java", "java.lang.StringBuilder").init();
var matcher = createObject("java", "java.util.regex.Pattern").compile(stripRegEx).matcher(stringToStrip);
var last = 0;
while ( condition=matcher.find() ) {
// append content before next capture
result.append(
stringToStrip.substring(
last,
matcher.start()
)
);
// full tag capture
capture = matcher.group(
javaCast("int", 0)
);
// keep only tags contained within the ignore regex
if (
len( arguments.reIgnore ) &&
reFindNoCase( arguments.reIgnore, capture )
) {
result.append( capture );
}
// continue at last cursor
last = matcher.end();
}
// append remaining content
result.append(
stringToStrip.substring( last )
);
return result.toString();
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment