Jason Avaya
Jason Avaya

Reputation: 51

How to strip ALL HTML tags using MSHTML Parser in VB6?

How to strip ALL HTML tags using MSHTML Parser in VB6?

Upvotes: 3

Views: 6523

Answers (3)

user3631132
user3631132

Reputation: 1

Public Function ParseHtml(ByVal str As String) As String
    Dim Ret As String, TagOpenend As Boolean, TagClosed As Boolean
    Dim n As Long, sChar As String
    For n = 1 To Len(str)
        sChar = Mid(str, n, 1)
        Select Case sChar
            Case "<"
                TagOpenend = True
            Case ">"
                TagClosed = True
                TagOpenend = False
            Case Else
                If TagOpenend = False Then
                    Ret = Ret & sChar
                End If
        End Select
    Next
    ParseHtml = Ret
End Function

This is a simple function i mafe for my own use. use Debug window

?ParseHtml( "< div >test< /div >" )

test

I hope this will help without using external libraries

Upvotes: 0

Alex K.
Alex K.

Reputation: 175748

One way:

Function strip(html As String) As String
    With CreateObject("htmlfile")
        .Open
        .write html
        .Close
        strip = .body.outerText
    End With
End Function

For

?strip("<strong>hello <i>wor<u>ld</u>!</strong><foo> 1234")
hello world! 1234

Upvotes: 0

Jason Avaya
Jason Avaya

Reputation: 51

This is adapted from Code over at CodeGuru. Many Many thanks to the original author: http://www.codeguru.com/vb/vb_internet/html/article.php/c4815

Check the original source if you need to download your HTML from the web. E.g.:

Set objDocument = objMSHTML.createDocumentFromUrl("http://google.com", vbNullString)

I don't need to download the HTML stub from the web - I already had my stub in memory. So the original source didn't quite apply to me. My main goal is just to have a qualified DOM Parser strip the HTML from the User generated content for me. Some would say, "Why not just use some RegEx to strip the HTML?" Good luck with that!

Add a reference to: Microsoft HTML Object Library

This is the same HTML Parser that runs Internet Explorer (IE) - Let the heckling begin. Well, Heckle away...

Here's the code I used:

Dim objDocument As MSHTML.HTMLDocument
Set objDocument = New MSHTML.HTMLDocument

'NOTE: txtSource is an instance of a simple TextBox object
objDocument.body.innerHTML = "<p>Hello World!</p> <p>Hello Jason!</p> <br/>Hello Bob!"
txtSource.Text = objDocument.body.innerText

The resulting text in txtSource.Text is my User's Content stripped of all HTML. Clean and maintainable - No Cthulhu Way for me.

Upvotes: 1

Related Questions