Feb 09 2010

ASP equivalent to PHP strip_tags

Category: AspGiulio Pons @ 10:09 am

I’ve found those functions around in the internet and I put them here just to remind how to strip tags with ASP. In ASP there isn’t an equivalent of PHP strip_tags function, so, here there are two function that do this with regular expressions. The first function strips anything:

function strip_tags(strHTML)
	dim regEx
	Set regEx = New RegExp
	With regEx
		.Pattern = "<(.|\n)+?>"
		.IgnoreCase = true
		.Global = true
	End With
	strip_tags = regEx.replace(strHTML, "")
end function

The second one has an “allowed tags” parameter as the PHP function to keep some specified tags (the tags to keep must be comma separated). This second function is better since you can put allowedTags equal to an empty string to strip all the tags as the first function.

function strip_tags(strHTML, allowedTags)

	dim objRegExp, strOutput
	set objRegExp = new regexp

	strOutput = strHTML
	allowedTags = "," & lcase(replace(allowedTags, " ", "")) & ","

	objRegExp.IgnoreCase = true
	objRegExp.Global = true
	objRegExp.MultiLine = true
	objRegExp.Pattern = "<(.|\n)+?>"
	set matches = objRegExp.execute(strHTML)
	objRegExp.Pattern = "<(/?)(\w+)[^>]*>"
	for each match in matches
		tagName = objRegExp.Replace(match.value, "$2")
		if instr(allowedTags, "," & lcase(tagName) & ",") = 0 then
			strOutput = replace(strOutput, match.value, "")
		end if
	next
	strip_tags = strOutput
	set objRegExp = nothing
end function
Share

Related posts:

  1. ASP equivalent to PHP ereg_replace function
  2. ASP equivalent to PHP Ucfirst function
  3. PHP Web page to text function
  4. Php Function to resize video from YouTube and Vimeo
  5. Get URL parameter in javascript

Tags: , , ,

One Response to “ASP equivalent to PHP strip_tags”

  1. Dentists Poland says:

    Howdy just wanted to give you a brief heads up and let you know a few of the pictures aren’t loading correctly. I’m not sure why but I think its a linking issue. I’ve tried it in two different internet browsers and both show the same outcome.

Leave a Reply