Skip to content

Instantly share code, notes, and snippets.

@gwobcke
Created June 15, 2011 13:51
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gwobcke/1027133 to your computer and use it in GitHub Desktop.
Save gwobcke/1027133 to your computer and use it in GitHub Desktop.
Classic ASP Strip HTML Function
<%
FUNCTION stripHTML(strHTML)
Dim objRegExp, strOutput, tempStr
Set objRegExp = New Regexp
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = "<(.|n)+?>"
'Replace all HTML tag matches with the empty string
strOutput = objRegExp.Replace(strHTML, "")
'Replace all < and > with &lt; and &gt;
strOutput = Replace(strOutput, "<", "&lt;")
strOutput = Replace(strOutput, ">", "&gt;")
stripHTML = strOutput 'Return the value of strOutput
Set objRegExp = Nothing
END FUNCTION
%>
@theugurcan
Copy link

Thanks for the function it works

@gcorwin
Copy link

gcorwin commented Nov 13, 2019

Sweet, I had one but this is much better. Thanks

@Taeil-Yi
Copy link

Taeil-Yi commented Sep 1, 2021

If the string contains the string "<spec...>" it will be removed. I don't think it's a valid function.
Should we warn users not to use "<" and ">" individually?

@gwobcke
Copy link
Author

gwobcke commented Sep 1, 2021

The purpose of the function is to strip HTML markup from a defined string. It is not meant to be a parser of markup nor a markup validator, however providing a string like "This is a <strong><spec></strong> tag" would be invalid markup in any case and as such should already be correctly encoded like "This is a <strong>&lt;spec&gt;</strong> tag". EDIT: even these comments filter like this, hence my edit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment