How To Strip All Html Tags Using Mshtml Parser In Vb6?
Solution 1:
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.:
SetobjDocument= 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.
Solution 2:
One way:
Function strip(html AsString) AsStringWith CreateObject("htmlfile")
.Open
.write html
.Close
strip = .body.outerText
EndWithEndFunction
For
?strip("<strong>hello <i>wor<u>ld</u>!</strong><foo> 1234")
hello world! 1234
Solution 3:
PublicFunction ParseHtml(ByVal str AsString) AsStringDim Ret AsString, TagOpenend AsBoolean, TagClosed AsBooleanDim n AsLong, sChar AsStringFor n = 1To Len(str)
sChar = Mid(str, n, 1)
SelectCase sChar
Case"<"
TagOpenend = TrueCase">"
TagClosed = True
TagOpenend = FalseCaseElseIf TagOpenend = FalseThen
Ret = Ret & sChar
EndIfEndSelectNext
ParseHtml = Ret
EndFunction
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
Post a Comment for "How To Strip All Html Tags Using Mshtml Parser In Vb6?"