Search Tools Links Login

HTML2Text


This code is for converting a string with HTML tags and encodings into a text-only string. It rids multiple spaces and supports ALL encoded characters like ",  , Ä and so on.

Original Author: unknown

Inputs

The original HTML String OrigHTML$

Returns

Text-only string

Code

Public Function HTML2Text(ByVal OrigHTML$) As String
On Error Resume Next
If InStr(LCase$(OrigHTML$), " 0 Then
OrigHTML$ = Mid$(OrigHTML$, InStr(LCase$(OrigHTML$), " OrigHTML$ = Mid$(OrigHTML$, InStr(OrigHTML$, ">") + 1)
If InStr(LCase$(OrigHTML$), "") > 0 Then _
  OrigHTML$ = Left$(OrigHTML$, InStr(LCase$(OrigHTML$), "") - 1)
End If
Do While Len(OrigHTML$)
CurrChar$ = Left$(OrigHTML$, 1)
OrigHTML$ = Mid$(OrigHTML$, 2)
Select Case CurrChar$
Case " "
  OrigHTML$ = LTrim$(OrigHTML$)
Case vbCr, vbLf
  CurrChar$ = ""
  If Left$(OrigHTML$, 1) = vbLf Then OrigHTML$ = Mid$(OrigHTML$, 2)
  OrigHTML$ = LTrim$(OrigHTML$)
Case "<"
  CurrChar$ = ""
  If InStr(OrigHTML$, ">") > 0 Then
   CurrChar$ = Left$(OrigHTML$, InStr(OrigHTML$, ">") - 1)
   OrigHTML$ = Mid$(OrigHTML$, InStr(OrigHTML$, ">") + 1)
  
   Select Case LCase$(CurrChar$)
   Case "p", "/div"
    CurrChar$ = vbCrLf + vbCrLf
   Case "br"
    CurrChar$ = vbCrLf
   Case Else
    CurrChar$ = ""
   End Select
  End If
Case "&"
  If InStr(OrigHTML$, ";") > 0 And InStr(OrigHTML$, ";") < InStr(OrigHTML$, " ") Then
   CurrChar$ = Left$(OrigHTML$, InStr(OrigHTML$, ";") - 1)
   OrigHTML$ = Mid$(OrigHTML$, InStr(OrigHTML$, ";") + 1)
  
   Select Case CurrChar$
   Case "amp"
    CurrChar$ = "&"
   Case "quot"
    CurrChar$ = """"
   Case "lt"
    CurrChar$ = "<"
   Case "gt"
    CurrChar$ = ">"
   Case "nbsp"
    CurrChar$ = " "
   Case "Auml"
    CurrChar$ = ""
   Case "auml"
    CurrChar$ = ""
   Case "iexcl"
    CurrChar$ = ""
   Case "cent"
    CurrChar$ = ""
   Case "pound"
    CurrChar$ = ""
   Case "curren"
    CurrChar$ = ""
   Case "yen"
    CurrChar$ = ""
   Case "brvbar"
    CurrChar$ = "|"
   Case "sect"
    CurrChar$ = ""
   Case "uml"
    CurrChar$ = ""
   Case "copy"
    CurrChar$ = ""
   Case "ordf"
    CurrChar$ = ""
   Case "laquo"
    CurrChar$ = ""
   Case "not"
    CurrChar$ = ""
   Case "reg"
    CurrChar$ = ""
   Case "macr"
    CurrChar$ = ""
   Case "deg"
    CurrChar$ = ""
   Case "plusm"
    CurrChar$ = ""
   Case "sup2"
    CurrChar$ = ""
   Case "sup3"
    CurrChar$ = ""
   Case "acute"
    CurrChar$ = ""
   Case "micro"
    CurrChar$ = ""
   Case "para"
    CurrChar$ = ""
   Case "middot"
    CurrChar$ = ""
   Case "cedil"
    CurrChar$ = ""
   Case "sup1"
    CurrChar$ = ""
   Case "ordm"
    CurrChar$ = ""
   Case "raquo"
    CurrChar$ = ""
   Case "frac14"
    CurrChar$ = ""
   Case "frac12"
    CurrChar$ = ""
   Case "frac34"
    CurrChar$ = ""
   Case "iquest"
    CurrChar$ = ""
   Case "Agrave"
    CurrChar$ = ""
   Case "Aacute"
    CurrChar$ = ""
   Case "Acirc"
    CurrChar$ = ""
   Case "Atilde"
    CurrChar$ = ""
   Case "Aring"
    CurrChar$ = ""
   Case "AElig"
    CurrChar$ = ""
   Case "Ccedil"
    CurrChar$ = ""
   Case "Egrave"
    CurrChar$ = ""
   Case "Eacute"
    CurrChar$ = ""
   Case "Ecirc"
    CurrChar$ = ""
   Case "Euml"
    CurrChar$ = ""
   Case "Igrave"
    CurrChar$ = ""
   Case "Iacute"
    CurrChar$ = ""
   Case "Icirc"
    CurrChar$ = ""
   Case "Iuml"
    CurrChar$ = ""
   Case "ETH"
    CurrChar$ = ""
   Case "Ntilde"
    CurrChar$ = ""
   Case "Ograve"
    CurrChar$ = ""
   Case "Oacute"
    CurrChar$ = ""
   Case "Ocirc"
    CurrChar$ = ""
   Case "Otilde"
    CurrChar$ = ""
   Case "Ouml"
    CurrChar$ = ""
   Case "times"
    CurrChar$ = ""
   Case "Oslash"
    CurrChar$ = ""
   Case "Ugrave"
    CurrChar$ = ""
   Case "Uacute"
    CurrChar$ = ""
   Case "Ucirc"
    CurrChar$ = ""
   Case "Uuml"
    CurrChar$ = ""
   Case "Yacute"
    CurrChar$ = ""
   Case "THORN"
    CurrChar$ = ""
   Case "szlig"
    CurrChar$ = ""
   Case "agrave"
    CurrChar$ = ""
   Case "aacute"
    CurrChar$ = ""
   Case "acirc"
    CurrChar$ = ""
   Case "atilde"
    CurrChar$ = ""
   Case "aring"
    CurrChar$ = ""
   Case "aelig"
    CurrChar$ = ""
   Case "ccedil"
    CurrChar$ = ""
   Case "egrave"
    CurrChar$ = ""
   Case "eacute"
    CurrChar$ = ""
   Case "ecirc"
    CurrChar$ = ""
   Case "euml"
    CurrChar$ = ""
   Case "igrave"
    CurrChar$ = ""
   Case "iacute"
    CurrChar$ = ""
   Case "icirc"
    CurrChar$ = ""
   Case "iuml"
    CurrChar$ = ""
   Case "eth"
    CurrChar$ = ""
   Case "ntilde"
    CurrChar$ = ""
   Case "ograve"
    CurrChar$ = ""
   Case "oacute"
    CurrChar$ = ""
   Case "ocirc"
    CurrChar$ = ""
   Case "otilde"
    CurrChar$ = ""
   Case "ouml"
    CurrChar$ = ""
   Case "divide"
    CurrChar$ = ""
   Case "oslash"
    CurrChar$ = ""
   Case "ugrave"
    CurrChar$ = ""
   Case "uacute"
    CurrChar$ = ""
   Case "ucirc"
    CurrChar$ = ""
   Case "uuml"
    CurrChar$ = ""
   Case "yacute"
    CurrChar$ = ""
   Case "thorn"
    CurrChar$ = ""
   Case "yuml"
    CurrChar$ = ""
   Case Else
    CurrChar$ = "&" + CurrChar$ + ";"
   End Select
  End If
End Select
NoHTML$ = NoHTML$ + CurrChar$
Loop
HTML2Text = NoHTML$
End Function

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 95 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.