How to remove html in asp

藏色散人
Release: 2023-01-06 11:12:29
Original
2873 people have browsed it

aspMethods to remove html: 1. Disable the html tag directly; 2. Use the "function RemoveHTML(){...}" method to remove; 3. Use IE or other tools to remove; 4. Remove through VBScript HTML code.

How to remove html in asp

The operating environment of this article: Windows7 system, HTML5&&ASP3.0 version, Dell G3 computer.

ASP removes HTML code:

Method 1: Disable HTML

The easiest way is to directly disable html tags without removing them. You can use the Replace() function. For example:

strText = Replace(strText, "<script", "<script", 1, -1, 1)
Copy after login

Or directly disable all html:

strText = Replace(strText, "<", "<")
Copy after login

Although this is safe, it is not friendly enough. (The text submitted by the user will become difficult to read)

Method 2: Using "<" and ">"

How to make the html tag disappear from the text? We can remove everything between "<" and ">"

In JavaScript this is simple:

function RemoveHTML( strText )
{
var regEx = /<[^>]*>/g;
return strText.replace(regEx, "");
}
Copy after login

Now back to VBScript, for Scripting engine 5.0 or higher (the version can be verified by calling the ScriptEngineMajorVersion and ScriptEngineMinorVersion functions), we 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
Copy after login

If regular expressions are not used, the following function 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
Copy after login

Although the above methods can remove the html tags in brackets, these methods have the following problems:

First, any angle brackets within the text that do not represent html will be removed. And the text between the two angle brackets will also be deleted. In other words, any "<" or " >" will produce unpredictable results.

In addition, this method cannot control which html tags are deleted. For example, these harmless tags are usually allowed.

Method Three: Using 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)
Copy after login

Method Four: VBScript

The following functions can be restricted to specific html tags

Introduction:

To control the deleted tag list, you can add/remove tags to the TAGLIST constant. For example, if you want to keep all tags, delete them from TAGLIST B. The current list contains all html tags and LAYER tags in MSDN. Each tag should be enclosed by ";".

The start tag and end tag will be deleted, such as "" and

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 closing tags are not considered html tags, and their content will not be deleted

If a block tag does not have an ending tag, all content from the beginning of this tag to the end of the text will be deleted

If the character following "