2011-09-14 68 views
0

有人給我發了一個程序中,我將需要更換與他們的編碼版本的HTML特殊編碼的所有字符(例如改變所有',或所有ÀÀ)。與HTML編碼正確對待在ACCESS

我不知道爲什麼需要它,但我不做決定,我必須這樣做。我想知道是否有一種方法可以快速有效地替換所有這些字符或者儘可能少地使用代碼。

爲了讓更多的東西「有趣」,整個事情是在Access 2003中

我可能會做這樣的事情

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

End Function 

回答

3

在這裏你去,這應該做你需要的東西:

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 
+0

我正想要這樣的事情,但我希望能找到更有效率或更「可愛」的東西。此外,這種方式很容易忘記一些,就像我看到的那樣,因爲'不在那裏。 –

+0

另一種選擇是從文件加載它們,這會使代碼更清潔,但效率也會低得多。同樣,將它們存儲在某種數組中,然後循環遍歷數組在代碼中看起來會更好,但也會佔用更多的內存。 – EdoDodo

+0

好啊。這個解決方案會做:)。我想我相信Acess可能已經實現了它,這是天真的。 –

2

已回答的問題,但我也想說點什麼。
如果願意,可以快速生成基於十六進制的html實體。
這種方式更容易不知道實體名稱。

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