Remove HTML tags

Source: Internet
Author: User
Tags object end functions html tags

Method One: Disable HTML
The easiest way to do this is to disable HTML tags directly without removing them. You can use the Replace () function. For example:

StrText = Replace (StrText, "<script", "<script", 1,-1, 1)

or disable all HTML directly:
StrText = Replace (StrText, "<", "<")

Although it is safe, it is not friendly enough. (User submitted text will become difficult to read)

Method Two: Use "<" and ">"
How do you make HTML tags disappear from the text? We can get rid of everything between "<" and ">"

This is simple in javascript:

function removehtml (strText)
{
var regEx =/<[^>]*>/g;
Return Strtext.replace (RegEx, "");
}

About regular expressions can refer to: http://developer.netscape.com/docs/manuals/js/client/jsguide/regexp.htm.

Now back to VBScript, for scripting Engine 5.0 or later (you can verify the version by invoking the ScriptEngineMajorVersion and ScriptEngineMinorVersion functions), We can also use RegExp objects:

Function removehtml (StrText)
Dim RegEx

Set RegEx = New RegExp

Regex.pattern = "<[^>]*>"
Regex.global = True

removehtml = Regex.Replace (StrText, "")
End Function

If you do not use regular expressions, 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
  &nb sp;         Exit do
        end If
        nPos1 = InStr (StrText, "<")
    Loop
 & nbsp; 
    removehtml = StrText
End Function
The above methods can remove the HTML tags in parentheses, but these methods all have the following problems:

First, any angle brackets within the text that do not represent HTML are removed. And the text in the middle of the two brackets is deleted. In other words, inserting any "<" or ">" in the text will result in unpredictable results.

In addition, this method does not control which HTML tags are deleted. For example <b><i> These harmless labels are usually allowed.

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

"It May is 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 could not function PR Operly in the 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 IV: VBScript
The following functions can be restricted to specific HTML tags
Brief introduction:

To control the list of deleted labels, you can do so by adding/removing tags to the TagList constant. For example, to retain all <B> labels, remove B from the TagList. The current list contains all the HTML tags and LAYER tags in MSDN. Each label should be used ";" Enclosed.
Both the start and end tags are deleted, such as "<A...>" and </A...>
If the label is in both the TagList and the Blocktaglist constants, all content between the start and end tags will be deleted
A label without an end tag is not treated as an HTML tag and its contents are not deleted
If there is no end tag for the Block label, everything from the beginning of the label to the end of the text will be deleted
If the character followed by "<!--" is not a space, the annotation label will not be deleted
Using this function is simple:
Strplaintext = removehtml (strtextwithhtml)
The contents of the function are 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



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.