开发者

Correctly deal with HTML encoding in ACCESS

开发者 https://www.devze.com 2023-04-05 01:05 出处:网络
Somebody sent me a program in which I would need to replace all characters that have special encoding in HTML with their encoded version (e.g. change all \' to ', or all À to À).

Somebody sent me a program in which I would need to replace all characters that have special encoding in HTML with their encoded version (e.g. change all ' to ', or all À to À).

I'm not sure why it's needed, but I don't make the decision and I have to do it. I was wondering if there was a way to quickly and efficiently replace all those characters at once or with the least amount of code possible.

To make thing more 'interesting' the whole thing is in ACCESS 2003.

I will probably do som开发者_运维问答ething like that

Private Function EncodeString(strOriginal as string) as string
'replace the characters with Replace or something of the sort.    

End Function


Here you go, this should do what you need:

Private Function EncodeString(strOriginal as string) as string
    Dim strTemp as string
    strTemp = strOriginal
    strTemp = Replace(strTemp, "ó", "ó")
    strTemp = Replace(strTemp, "á", "á")
    strTemp = Replace(strTemp, "é", "é")
    strTemp = Replace(strTemp, "í", "í")
    strTemp = Replace(strTemp, "ú", "ú")
    strTemp = Replace(strTemp, "Á", "Á")
    strTemp = Replace(strTemp, "É", "É")
    strTemp = Replace(strTemp, "Í", "Í")
    strTemp = Replace(strTemp, "Ó", "Ó")
    strTemp = Replace(strTemp, "Ú", "Ú")
    strTemp = Replace(strTemp, "¡", "¡")
    strTemp = Replace(strTemp, "¢", "¢")
    strTemp = Replace(strTemp, "£", "£")
    strTemp = Replace(strTemp, "¤", "¤")
    strTemp = Replace(strTemp, "¥", "¥")
    strTemp = Replace(strTemp, "¦", "¦")
    strTemp = Replace(strTemp, "§", "§")
    strTemp = Replace(strTemp, "¨", "¨")
    strTemp = Replace(strTemp, "©", "©")
    strTemp = Replace(strTemp, "ª", "ª")
    strTemp = Replace(strTemp, "«", "«")
    strTemp = Replace(strTemp, "¬", "¬")
    strTemp = Replace(strTemp, "®", "®")
    strTemp = Replace(strTemp, "¯", "¯")
    strTemp = Replace(strTemp, "°", "°")
    strTemp = Replace(strTemp, "±", "±")
    strTemp = Replace(strTemp, "²", "²")
    strTemp = Replace(strTemp, "³", "³")
    strTemp = Replace(strTemp, "´", "´")
    strTemp = Replace(strTemp, "µ", "µ")
    strTemp = Replace(strTemp, "¶", "¶")
    strTemp = Replace(strTemp, "·", "·")
    strTemp = Replace(strTemp, "¸", "¸")
    strTemp = Replace(strTemp, "¹", "¹")
    strTemp = Replace(strTemp, "º", "º")
    strTemp = Replace(strTemp, "»", "»")
    strTemp = Replace(strTemp, "¼", "¼")
    strTemp = Replace(strTemp, "½", "½")
    strTemp = Replace(strTemp, "¾", "¾")
    strTemp = Replace(strTemp, "¿", "¿")
    strTemp = Replace(strTemp, "×", "×")
    strTemp = Replace(strTemp, "÷", "÷")
    strTemp = Replace(strTemp, "À", "À")
    strTemp = Replace(strTemp, "Â", "Â")
    strTemp = Replace(strTemp, "Ã", "Ã")
    strTemp = Replace(strTemp, "Ä", "Ä")
    strTemp = Replace(strTemp, "Å", "Å")
    strTemp = Replace(strTemp, "Æ", "Æ")
    strTemp = Replace(strTemp, "Ç", "Ç")
    strTemp = Replace(strTemp, "È", "È")
    strTemp = Replace(strTemp, "Ê", "Ê")
    strTemp = Replace(strTemp, "Ë", "Ë")
    strTemp = Replace(strTemp, "Ì", "Ì")
    strTemp = Replace(strTemp, "Î", "Î")
    strTemp = Replace(strTemp, "Ï", "Ï")
    strTemp = Replace(strTemp, "Ð", "Ð")
    strTemp = Replace(strTemp, "Ñ", "Ñ")
    strTemp = Replace(strTemp, "Ò", "Ò")
    strTemp = Replace(strTemp, "Ô", "Ô")
    strTemp = Replace(strTemp, "Õ", "Õ")
    strTemp = Replace(strTemp, "Ö", "Ö")
    strTemp = Replace(strTemp, "Ø", "Ø")
    strTemp = Replace(strTemp, "Ù", "Ù")
    strTemp = Replace(strTemp, "Û", "Û")
    strTemp = Replace(strTemp, "Ü", "Ü")
    strTemp = Replace(strTemp, "Ý", "Ý")
    strTemp = Replace(strTemp, "Þ", "Þ")
    strTemp = Replace(strTemp, "ß", "ß")
    strTemp = Replace(strTemp, "à", "à")
    strTemp = Replace(strTemp, "â", "â")
    strTemp = Replace(strTemp, "ã", "ã")
    strTemp = Replace(strTemp, "ä", "ä")
    strTemp = Replace(strTemp, "å", "å")
    strTemp = Replace(strTemp, "æ", "æ")
    strTemp = Replace(strTemp, "ç", "ç")
    strTemp = Replace(strTemp, "è", "è")
    strTemp = Replace(strTemp, "ê", "ê")
    strTemp = Replace(strTemp, "ë", "ë")
    strTemp = Replace(strTemp, "ì", "ì")
    strTemp = Replace(strTemp, "î", "î")
    strTemp = Replace(strTemp, "ï", "ï")
    strTemp = Replace(strTemp, "ð", "ð")
    strTemp = Replace(strTemp, "ñ", "ñ")
    strTemp = Replace(strTemp, "ò", "ò")
    strTemp = Replace(strTemp, "ô", "ô")
    strTemp = Replace(strTemp, "õ", "õ")
    strTemp = Replace(strTemp, "ö", "ö")
    strTemp = Replace(strTemp, "ø", "ø")
    strTemp = Replace(strTemp, "ù", "ù")
    strTemp = Replace(strTemp, "û", "û")
    strTemp = Replace(strTemp, "ü", "ü")
    strTemp = Replace(strTemp, "ý", "ý")
    strTemp = Replace(strTemp, "þ", "þ")
    strTemp = Replace(strTemp, "ÿ", "ÿ")
    EncodeString = strTemp
End Function


Question answered, but I would like to say something too.
If you would, you can generate hex based html entities quickly.
It's easier this way to not have know the entity name.

Function EncodeString(ByVal strOriginal)
    Dim currChar, i, sOut, CharList
    CharList =  "óáéíúÁÉÍÓÚ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿×÷ÀÂÃÄÅÆ" &_
                "ÇÈÊËÌÎÏÐÑÒÔÕÖØÙÛÜÝÞßàâãäåæçèêëìîïðñòôõöøùûüýþÿ"
    sOut = strOriginal
    For i = 1 To Len(CharList)
        currChar = Mid(CharList, i, 1)
        sOut = Replace(sOut, currChar, "&#x"& Hex(AscW(currChar)) &";")
    Next
    EncodeString = sOut
End Function
0

精彩评论

暂无评论...
验证码 换一张
取 消