Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB5,VB6
Expertise: Intermediate
Aug 11, 2001

HTMLDecodeEx - Decodes HTML encoded strings

' Decodes HTML encoding applied to the specified Text and returns the result
' Optionally specify if we encoded a HREF link and the character
' used for encoding - the default is the standard ampersand

' i.e. Call MsgBox(HTMLDecodeEx("<Sample HTML Decoded Text>"
' & ""))

Public Function HTMLDecodeEx(ByVal Text As String, Optional ByVal vfHREFDecode _
    As Boolean = False, Optional ByVal vstrAmpChar As String = "&") As String
    
    Dim astrTokens() As String
    Dim lngTotal As Long
    Dim lngCount As Long
    Dim strToken As String
    Dim strCode As String
    Dim lngPos As Long
    Dim astrReturn() As String
    Dim strReturn As String
    
    strReturn = Text
    
    If Trim$(strReturn) <> "" Then
        ' tokenize the text
        astrTokens = Split(Text, vstrAmpChar, -1, vbTextCompare)
            
        lngTotal = UBound(astrTokens)
        
        ' create enough space for the return
        ReDim astrReturn(lngTotal)
        
        For lngCount = 0 To lngTotal
            ' look for the end of the token
            strToken = astrTokens(lngCount)
            lngPos = InStr(1, strToken, ";", vbTextCompare)
            If (lngPos = 0) Then
                astrReturn(lngCount) = strToken
            Else
                strCode = Mid$(strToken, 1, lngPos)
                Select Case strCode
                    Case "nbsp;"
                        astrReturn(lngCount) = " " & Mid$(strToken, lngPos + 1)
                    Case "quot;"
                        astrReturn(lngCount) = """" & Mid$(strToken, lngPos + 1)
                    Case "amp;"
                        astrReturn(lngCount) = vstrAmpChar & Mid$(strToken, _
                            lngPos + 1)
                    Case "lt;"
                        astrReturn(lngCount) = "<" & Mid$(strToken, lngPos + 1)
                    Case "gt;"
                        astrReturn(lngCount) = ">" & Mid$(strToken, lngPos + 1)
                    Case Else
                        ' see if it's
                        If Left$(strToken, 1) = "#" Then
                            astrReturn(lngCount) = Chr$(Mid$(strToken, 2, _
                                lngPos - 2)) & Mid$(strToken, lngPos + 1)
                        Else
                            astrReturn(lngCount) = strToken
                        End If
                End Select
            End If
        Next ' lngCount
        
        Erase astrTokens
        
        strReturn = Join(astrReturn, "")
    
        Erase astrReturn
    End If
    
    HTMLDecodeEx = strReturn
    
End Function

'###################################
'#
'# This tip has been brought to you by www.omnisolvonline.com,
'# makers of a host of software development, energy and information 
'# management tools.
'#
'###################################

Paul McEwan
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date