Results 1 to 10 of 10

Thread: VB - Kills Html Tags

  1. #1

    Thread Starter
    Hyperactive Member BrandonTurner's Avatar
    Join Date
    Sep 2001
    Location
    East Lansing, Michiagn
    Posts
    268

    VB - Kills Html Tags

    VB Code:
    1. Public Function KillHTML(ByVal strIn As String) As String
    2.   Dim lngLen As Long, lngFound As Long, lngEnd As Long
    3.   Dim strLeft As String, strRight As String
    4.   strIn$ = Replace(strIn$, "<HTML>", "")
    5.   strIn$ = Replace(strIn$, "</HTML>", "")
    6.   strIn$ = Replace(strIn$, "<SUP>", "")
    7.   strIn$ = Replace(strIn$, "</SUP>", "")
    8.   strIn$ = Replace(strIn$, "<HR>", "")
    9.   strIn$ = Replace(strIn$, "<H1>", "")
    10.   strIn$ = Replace(strIn$, "<H2>", "")
    11.   strIn$ = Replace(strIn$, "<H3>", "")
    12.   strIn$ = Replace(strIn$, "<PRE>", "")
    13.   strIn$ = Replace(strIn$, "</PRE>", "")
    14.   strIn$ = Replace(strIn$, "<PRE=", "")
    15.   strIn$ = Replace(strIn$, "<B>", "")
    16.   strIn$ = Replace(strIn$, "</B>", "")
    17.   strIn$ = Replace(strIn$, "<U>", "")
    18.   strIn$ = Replace(strIn$, "</U>", "")
    19.   strIn$ = Replace(strIn$, "<I>", "")
    20.   strIn$ = Replace(strIn$, "</I>", "")
    21.   strIn$ = Replace(strIn$, "<FONT>", "")
    22.   strIn$ = Replace(strIn$, "</FONT>", "")
    23.   strIn$ = Replace(strIn$, "<BODY>", "")
    24.   strIn$ = Replace(strIn$, "</BODY>", "")
    25.   strIn$ = Replace(strIn$, "<BR>", "")
    26.   strIn$ = Replace(strIn$, "</A>", "")
    27.   lngLen& = Len(strIn$)
    28.   lngFound& = InStr(strIn$, "<BODY ")
    29.   Do While lngFound& <> 0
    30.     lngEnd& = InStr(lngFound&, strIn$, ">")
    31.     If lngEnd& <> 0 Then
    32.       strLeft$ = Left(strIn$, lngFound& - 1)
    33.       strRight$ = Right(strIn$, lngLen& - lngEnd&)
    34.       strIn$ = strLeft$ & strRight$
    35.       lngLen& = Len(strIn$)
    36.     End If
    37.     lngFound& = InStr(lngFound& + 1, strIn$, "<BODY ")
    38.   Loop
    39.   lngFound& = InStr(strIn$, "<A ")
    40.   Do While lngFound& <> 0
    41.     lngEnd& = InStr(lngFound&, strIn$, ">")
    42.     If lngEnd& <> 0 Then
    43.       strLeft$ = Left(strIn$, lngFound& - 1)
    44.       strRight$ = Right(strIn$, lngLen& - lngEnd&)
    45.       strIn$ = strLeft$ & strRight$
    46.       lngLen& = Len(strIn$)
    47.     End If
    48.     lngFound& = InStr(lngFound& + 1, strIn$, "<A ")
    49.   Loop
    50.   lngFound& = InStr(strIn$, "<FONT ")
    51.   Do While lngFound& <> 0
    52.     lngEnd& = InStr(lngFound&, strIn$, ">")
    53.     If lngEnd& <> 0 Then
    54.       strLeft$ = Left(strIn$, lngFound& - 1)
    55.       strRight$ = Right(strIn$, lngLen& - lngEnd&)
    56.       strIn$ = strLeft$ & strRight$
    57.       lngLen& = Len(strIn$)
    58.     End If
    59.     lngFound& = InStr(lngFound& + 1, strIn$, "<FONT ")
    60.   Loop
    61.   strIn$ = Replace(strIn$, "&", "&")
    62.   strIn$ = Replace(strIn$, "&lt;", "<")
    63.   KillHTML$ = strIn$
    64. End Function

  2. #2
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    What about something like this?

    VB Code:
    1. Function RemoveHTML(strHTML As String) As String
    2. Do
    3.     tagOpen = InStr(1, strHTML, "<")
    4.     tagClose = InStr(1, strHTML, ">")
    5.     strHTML = Replace(strHTML, Mid(strHTML, tagOpen, tagClose - tagOpen + 1), "")
    6.     Debug.Print strHTML
    7. Loop Until InStr(1, strHTML, "<") = 0
    8. RemoveHTML = strHTML
    9. End Function

  3. #3

    Thread Starter
    Hyperactive Member BrandonTurner's Avatar
    Join Date
    Sep 2001
    Location
    East Lansing, Michiagn
    Posts
    268
    your way is about 10x faster and effective. congrats

  4. #4
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    No worries,

    you could also replace html characters etc, if needed.

  5. #5
    Hyperactive Member
    Join Date
    May 2001
    Posts
    414
    Hey da_silvy.
    That does look like a fast way but how would you use it? For instance, if you had the Html in Text1. I've tried a lot of different way but just get an invalid procedure error.

  6. #6
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    VB Code:
    1. Text1.Text = RemoveHTML(Text1.Text)

    HTH

  7. #7
    Hyperactive Member
    Join Date
    May 2001
    Posts
    414
    Text1.Text = RemoveHTML(Text1.Text)


    That's the call I'm using but I get an "Invalid Procedure" on this line

    strHTML = Replace(strHTML, Mid(strHTML, tagOpen, tagClose - tagOpen + 1), "")

  8. #8
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    What text are you trying to kill tags in?

  9. #9
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    It looks like a missing closing tag (or opening tag).
    Frans

  10. #10
    Hyperactive Member
    Join Date
    May 2001
    Posts
    414
    What text are you trying to kill tags in?

    I've loaded the Html code from a web page into Text1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width