How to remove HTML tags in ASP

Source: Internet
Author: User
Method 1: Disable html
The simplest way is to directly disable HTML tags without removing them. You can use the Replace () function. For example:

Strtext = Replace (strtext, "<script", "& lt; script", 1,-1, 1)

Or directly disable all HTML:
Strtext = Replace (strtext, "<", "& lt ;")

Although this is safe, it seems unfriendly (the text submitted by the user will become difficult to read)

Method 2: Use "<" and ">"
How does one remove HTML tags from text? We can remove all content in the middle of "<" and "> ".

In JavaScript, this is simple:

Function removehtml (strtext)
{
VaR RegEx =/<[^>] *>/g;
Return strtext. Replace (RegEx ,"");
}

For more information about regular expressions, see: http://developer.netscape.com/docs/manuals/js/client/jsguide/regexp.htm.

Now back to VBScript. For the scripting engine version 5.0 or later (you can call the scriptenginemajorversion and scriptengineminorversion functions to check the version), you can also use the Regexp object:

Function removehtml (strtext)
Dim RegEx

Set RegEx = new Regexp

RegEx. pattern = "<[^>] *>"
RegEx. Global = true

Removehtml = RegEx. Replace (strtext ,"")
End Function

Without a regular expression, the following functions can achieve the same purpose:

Function removehtml (strtext)
Dim npos1
Dim npos2

Npos1 = instr (strtext, "<")
Do While npos1> 0
Npos2 = instr (npos1 + 1, strtext, "> ")
If npos2> 0 then
Strtext = left (strtext, npos1-1) & Mid (strtext, npos2 + 1)
Else
Exit do
End if
Npos1 = instr (strtext, "<")
Loop

Removehtml = strtext
End Function
Although the preceding methods can remove the HTML tags in parentheses, these methods have the following problems:

First, any angle brackets in the text that do not represent HTML will be removed. the text between the two angle brackets will also be deleted. in other words, inserting any "<" or ">" in the text will produce unexpected results.

In addition, this method cannot control the HTML tags to be deleted. For example, <B> <I> these harmless tags are usually allowed.

Method 3: Use IE or other tools
There are many disadvantages:

"It may be desirable to parse HTML files inside a Web server process in response to a browser page request. however, the webbrowser control, DHTML Editing control, mshtml, and other Internet Explorer components may not function properly in an Active Server Pages (ASP) page or other application run in a web server application. "(http://support.microsoft.com/support/kb/articles/Q244/0/85.ASP? Ln = en-US & SD = gn & Fr = 0)

Method 4: VBScript
The following functions can be restricted to specific HTML tags:
Introduction:

To control the list of deleted tags, you can add/delete tags to the taglist constant. for example, to retain all <B> tags, delete B from the taglist. the current list contains all HTML tags and layer tags in msdn. each tag must be included in.
Both the start and end tags are deleted, for example, "<a...>" and </a...>
If the tag is in both the taglist and blocktaglist constants, all content between the start tag and the end tag will be deleted.
Tags without ending tags are not regarded as HTML tags and their contents are not deleted.
If the block tag does not end with the tag, all content starting from the tag to the end of the text will be deleted.
If "<! -- "The followed character is not a space, and the comment label will not be deleted
Using this function is simple:
Strplaintext = removehtml (strtextwithhtml)
The function content is as follows:

Function removehtml (strtext)
Dim taglist
Taglist = ";! --;! Doctype; A; acronym; address; applet; area; B; base; basefont ;"&_
"Bgsound; big; BLOCKQUOTE; body; BR; button; Caption; center; CITE; Code ;"&_
"Col; colgroup; comment; dd; del; dfn; Dir; div; dl; DT; em; embed; fieldset ;"&_
"Font; form; frame; frameset; head; H1; H2; H3; H4; H5; H6; HR; HTML; I; IFRAME; IMG ;"&_
"Input; INS; isindex; KBD; label; layer; lagend; Li; Link; Listing; Map; marquee ;"&_
"Menu; Meta; nobr; noframes; NoScript; object; OL; option; P; Param; plaintext ;"&_
"Pre; q; s; Samp; script; select; small; span; strike; strong; style; sub; SUP ;"&_
"Table; tbody; TD; textarea; tfoot; th; thead; title; TR; TT; U; Ul; var; WBR; XMP ;"

const blocktaglist = "; applet; embed; frameset; head; noframes; NoScript; object; script; style; "
dim npos1
dim npos2
dim npos3
dim strresult
dim strtagname
dim bremove
dim bsearchforblock

npos1 = instr (strtext, "<")
do while npos1> 0
npos2 = instr (npos1 + 1, strtext, "> ")
If npos2> 0 then
strtagname = mid (strtext, npos1 + 1, npos2-npos1-1)
strtagname = Replace (replace (strtagname, vbcr, ""), vblf, "")

Npos3 = instr (strtagname ,"")
If npos3> 0 then
Strtagname = left (strtagname, npos3-1)
End if

If left (strtagname, 1) = "/" then
Strtagname = mid (strtagname, 2)
Bsearchforblock = false
Else
Bsearchforblock = true
End if

If instr (1, taglist, ";" & strtagname & ";", vbtextcompare)> 0 then
Bremove = true
If bsearchforblock then
If instr (1, blocktaglist, ";" & strtagname & ";", vbtextcompare)> 0 then
Npos2 = Len (strtext)
Npos3 = instr (npos1 + 1, strtext, "</" & strtagname, vbtextcompare)
If npos3> 0 then
Npos3 = instr (npos3 + 1, strtext, "> ")
End if

If npos3> 0 then
Npos2 = npos3
End if
End if
End if
Else
Bremove = false
End if

If bremove then
Strresult = strresult & left (strtext, npos1-1)
Strtext = mid (strtext, npos2 + 1)
Else
Strresult = strresult & left (strtext, npos1)
Strtext = mid (strtext, npos1 + 1)
End if
Else
Strresult = strresult & strtext
Strtext = ""
End if

Npos1 = instr (strtext, "<")
Loop
Strresult = strresult & strtext

Removehtml = strresult
End Function
---------------------------------------------

Note: I have translated an article on www.codeproject.com for foreigners.Article.

--------------------------------------------

Function striphtml (strtext)
Dim arysplit, I, j, stroutput
Arysplit = Split (strtext, "<")
If Len (arysplit (0)> 0 then j = 1 else J = 0
For I = J to ubound (arysplit)
If instr (arysplit (I), ">") then
Arysplit (I) = mid (arysplit (I), instr (arysplit (I), ">") + 1)
Else
Arysplit (I) = "<" & arysplit (I)
End if
Next
Stroutput = join (arysplit ,"")
Stroutput = mid (stroutput, 2-j)
Stroutput = Replace (stroutput, ">", "> ")
Stroutput = Replace (stroutput, "<", "<")

Striphtml = stroutput
End Function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.