ASP equivalent to PHP strip_tags

I’ve found those functions around in the internet and I put them here just to remind how to strip tags…

February 9, 2010

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

Author

I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of Rockit.it and Dailybest.it and I'm actually the Chief Technical Officer of Better Days web agency.

Comments on “ASP equivalent to PHP strip_tags”

One thought

  1. 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.

Comments are closed

Recommended

ASP equivalent to PHP ereg_replace function

I’ve used so many time the php function ereg_replace that when I have to use ASP (’cause sometimens you have…

November 10, 2009

ASP equivalent to PHP Ucfirst function

This code does the same thing that Ucfirst function makes in PHP. “Ucfirst” means “Uppercase first letter”. ASP doesn’t have…

Highlight text for search results in PHP

Useful code to highlight text occurences in search results or in a text. How to highlight text in a string…

September 2, 2016

How to add rel=”nofollow” to links with preg_replace()

Adding rel="nofollow" to external link is a good SEO practice.

September 22, 2015

Block junk emails, spammers and temporary emails

If you need an Email Validator Function, consider this version that includes also the check against common temporary mail services…

December 3, 2013

How many times a web link has been shared on Twitter

Twitter share button and Facebook share button are the most used buttons to share links on Internet. You can read…

October 19, 2012