HTMLentities

dcfax

New Member
Licensed User
Longtime User
i try to find a way to convert a string in html entities
since i don't find any easy solution, i write this

did anybody know a better way to do it ?

B4X:
Sub htmlentities(tc As String) As String
Dim res,testc As String
Dim n As Int
res=""
For n=0 To tc.Length-1
   testc=tc.CharAt(n)
   Select tc
   Case Chr(0022)
      res=res&"""'   quotation mark
   Case "'"
      res=res&"'"'   apostrophe 
   Case "&"
      res=res&"&"'   ampersand
   Case "<"
      res=res&"&lt;"'   less-than
   Case ">"
      res=res&"&gt;"'   greater-than
   Case " "
      res=res&"&nbsp;"'   non-breaking space
   Case "¡"
      res=res&"&iexcl;"'   inverted exclamation mark
   Case "¢"
      res=res&"&cent;"'   cent
   Case "£"
      res=res&"&pound;"'   pound
   Case "¤"
      res=res&"&curren;"'   currency
   Case "¥"
      res=res&"&yen;"'   yen
   Case "¦"
      res=res&"&brvbar;"'   broken vertical bar
   Case "§"
      res=res&"&sect;"'   section
   case "¨"
      res=res&"&uml;"'   spacing diaeresis
   case "©"   
      res=res&"&copy;"'   copyright
   case "ª"
      res=res&"&ordf;"'   feminine ordinal indicator
   case "«"
      res=res&"&laquo;"'   angle quotation mark (left)
   case "¬"
      res=res&"&not;"'   negation
   case "®"
      res=res&"&reg;"'   registered trademark
   case "¯"
      res=res&"&macr;"'   spacing macron
   case "°"
      res=res&"&deg;"'   degree
   case "±"
      res=res&"&plusmn;"'   plus-or-minus 
   case "²"
      res=res&"&sup2;"'   superscript 2
   case "³"
      res=res&"&sup3;"'   superscript 3
   case "´"
      res=res&"&acute;"'   spacing acute
   case "µ"
      res=res&"&micro;"'   micro
   case "¶"
      res=res&"&para;"'   paragraph
   case "·"
      res=res&"&middot;"'   middle dot
   case "¸"
      res=res&"&cedil;"'   spacing cedilla
   case "¹"
      res=res&"&sup1;"'   superscript 1
   case "º"
      res=res&"&ordm;"'   masculine ordinal indicator
   case "»"
      res=res&"&raquo;"'   angle quotation mark (right)
   case "¼"
      res=res&"&frac14;"'   fraction 1/4
   case "½"
      res=res&"&frac12;"'   fraction 1/2
   case "¾"
      res=res&"&frac34;"'   fraction 3/4
   case "¿"
      res=res&"&iquest;"'   inverted question mark
   case "×"
      res=res&"&times;"'   multiplication
   case "÷"
      res=res&"&divide;"'   division
   case "À"
      res=res&"&Agrave;"'   capital a, grave accent
   case "Á"
      res=res&"&Aacute;"'   capital a, acute accent
   case "Â"
      res=res&"&Acirc;"'   capital a, circumflex accent
   case "Ã"
      res=res&"&Atilde;"'   capital a, tilde
   case "Ä"
      res=res&"&Auml;"'   capital a, umlaut mark
   case "Å"
      res=res&"&Aring;"'   capital a, ring
   case "Æ"
      res=res&"&AElig;"'   capital ae
   case "Ç"
      res=res&"&Ccedil;"'   capital c, cedilla
   case "È"
      res=res&"&Egrave;"'   capital e, grave accent
   case "É"
      res=res&"&Eacute;"'   capital e, acute accent
   case "Ê"
      res=res&"&Ecirc;"'   capital e, circumflex accent
   case "Ë"
      res=res&"&Euml;"'   capital e, umlaut mark
   case "Ì"
      res=res&"&Igrave;"'   capital i, grave accent
   case "Í"
      res=res&"&Iacute;"'   capital i, acute accent
   case "Î"
      res=res&"&Icirc;"'   capital i, circumflex accent
   case "Ï"
      res=res&"&Iuml;"'   capital i, umlaut mark
   case "Ð"
      res=res&"&ETH;"'   capital eth, Icelandic
   case "Ñ"
      res=res&"&Ntilde;"'   capital n, tilde
   case "Ò"
      res=res&"&Ograve;"'   capital o, grave accent
   case "Ó"
      res=res&"&Oacute;"'   capital o, acute accent
   case "Ô"
      res=res&"&Ocirc;"'   capital o, circumflex accent
   case "Õ"
      res=res&"&Otilde;"'   capital o, tilde
   case "Ö"
      res=res&"&Ouml;"'   capital o, umlaut mark
   case "Ø"
      res=res&"&Oslash;"'   capital o, slash
   case "Ù"
      res=res&"&Ugrave;"'   capital u, grave accent
   case "Ú"
      res=res&"&Uacute;"'   capital u, acute accent
   case "Û"
      res=res&"&Ucirc;"'   capital u, circumflex accent
   case "Ü"
      res=res&"&Uuml;"'   capital u, umlaut mark
   case "Ý"
      res=res&"&Yacute;"'   capital y, acute accent
   case "Þ"
      res=res&"&THORN;"'   capital THORN, Icelandic
   case "ß"
      res=res&"&szlig;"'   small sharp s, German
   case "à"
      res=res&"&agrave;"'   small a, grave accent
   case "á"
      res=res&"&aacute;"'   small a, acute accent
   case "â"
      res=res&"&acirc;"'   small a, circumflex accent
   case "ã"
      res=res&"&atilde;"'   small a, tilde
   case "ä"
      res=res&"&auml;"'   small a, umlaut mark
   case "å"
      res=res&"&aring;"'   small a, ring
   case "æ"
      res=res&"&aelig;"'   small ae
   case "ç"
      res=res&"&ccedil;"'   small c, cedilla
   case "è"
      res=res&"&egrave;"'   small e, grave accent
   case "é"
      res=res&"&eacute;"'   small e, acute accent
   case "ê"
      res=res&"&ecirc;"'   small e, circumflex accent
   case "ë"
      res=res&"&euml;"'   small e, umlaut mark
   case "ì"
      res=res&"&igrave;"'   small i, grave accent
   case "í"
      res=res&"&iacute;"'   small i, acute accent
   case "î"
      res=res&"&icirc;"'   small i, circumflex accent
   case "ï"
      res=res&"&iuml;"'   small i, umlaut mark
   case "ð"
      res=res&"&eth;"'   small eth, Icelandic
   case "ñ"
      res=res&"&ntilde;"'   small n, tilde
   case "ò"
      res=res&"&ograve;"'   small o, grave accent
   case "ó"
      res=res&"&oacute;"'   small o, acute accent
   case "ô"
      res=res&"&ocirc;"'   small o, circumflex accent
   case "õ"
      res=res&"&otilde;"'   small o, tilde
   case "ö"
      res=res&"&ouml;"'   small o, umlaut mark
   case "ø"
      res=res&"&oslash;"'   small o, slash
   case "ù"
      res=res&"&ugrave;"'   small u, grave accent
   case "ú"
      res=res&"&uacute;"'   small u, acute accent
   case "û"
      res=res&"&ucirc;"'   small u, circumflex accent
   case "ü"
      res=res&"&uuml;"'   small u, umlaut mark
   case "ý"
      res=res&"&yacute;"'   small y, acute accent
   case "þ"
      res=res&"&thorn;"'   small thorn, Icelandic
   case "ÿ"
      res=res&"&yuml;"'   small y, umlaut mark
   case "€"
      res=res&"&euro;"
   Case "™"
      res=res&"&trade;"
   Case Else
      res=res & testc
   End Select
Next
Return res
End Sub
 

dcfax

New Member
Licensed User
Longtime User
Thanks Erel,
much more efficient that way !

B4X:
Sub htmlentities(tc As String) As String
Dim res As StringBuilder
Dim testc As String
Dim n As Int
res.Initialize
For n=0 To tc.Length-1
   testc=tc.CharAt(n)
   Select tc
   Case Chr(0022)
      res.append("&quot;")'   quotation mark
   Case "'"
      res.append("&apos;")'   apostrophe 
   Case "&"
      res.append("&amp;")'   ampersand
   Case "<"
      res.append("&lt;")'   less-than
   Case ">"
      res.append("&gt;")'   greater-than
   Case " "
      res.append("&nbsp;")'   non-breaking space
   Case "¡"
      res.append("&iexcl;")'   inverted exclamation mark
   Case "¢"
      res.append("&cent;")'   cent
   Case "£"
      res.append("&pound;")'   pound
   Case "¤"
      res.append("&curren;")'   currency
   Case "¥"
      res.append("&yen;")'   yen
   Case "¦"
      res.append("&brvbar;")'   broken vertical bar
   Case "§"
      res.append("&sect;")'   section
   Case "¨"
      res.append("&uml;")'   spacing diaeresis
   Case "©"   
      res.append("&copy;")'   copyright
   Case "ª"
      res.append("&ordf;")'   feminine ordinal indicator
   Case "«"
      res.append("&laquo;")'   angle quotation mark (left)
   Case "¬"
      res.append("&not;")'   negation
   Case "®"
      res.append("&reg;")'   registered trademark
   Case "¯"
      res.append("&macr;")'   spacing macron
   Case "°"
      res.append("&deg;")'   degree
   Case "±"
      res.append("&plusmn;")'   plus-or-minus 
   Case "²"
      res.append("&sup2;")'   superscript 2
   Case "³"
      res.append("&sup3;")'   superscript 3
   Case "´"
      res.append("&acute;")'   spacing acute
   Case "µ"
      res.append("&micro;")'   micro
   Case "¶"
      res.append("&para;")'   paragraph
   Case "·"
      res.append("&middot;")'   middle dot
   Case "¸"
      res.append("&cedil;")'   spacing cedilla
   Case "¹"
      res.append("&sup1;")'   superscript 1
   Case "º"
      res.append("&ordm;")'   masculine ordinal indicator
   Case "»"
      res.append("&raquo;")'   angle quotation mark (right)
   Case "¼"
      res.append("&frac14;")'   fraction 1/4
   Case "½"
      res.append("&frac12;")'   fraction 1/2
   Case "¾"
      res.append("&frac34;")'   fraction 3/4
   Case "¿"
      res.append("&iquest;")'   inverted question mark
   Case "×"
      res.append("&times;")'   multiplication
   Case "÷"
      res.append("&divide;")'   division
   Case "À"
      res.append("&Agrave;")'   capital a, grave accent
   Case "Á"
      res.append("&Aacute;")'   capital a, acute accent
   Case "Â"
      res.append("&Acirc;")'   capital a, circumflex accent
   Case "Ã"
      res.append("&Atilde;")'   capital a, tilde
   Case "Ä"
      res.append("&Auml;")'   capital a, umlaut mark
   Case "Å"
      res.append("&Aring;")'   capital a, ring
   Case "Æ"
      res.append("&AElig;")'   capital ae
   Case "Ç"
      res.append("&Ccedil;")'   capital c, cedilla
   Case "È"
      res.append("&Egrave;")'   capital e, grave accent
   Case "É"
      res.append("&Eacute;")'   capital e, acute accent
   Case "Ê"
      res.append("&Ecirc;")'   capital e, circumflex accent
   Case "Ë"
      res.append("&Euml;")'   capital e, umlaut mark
   Case "Ì"
      res.append("&Igrave;")'   capital i, grave accent
   Case "Í"
      res.append("&Iacute;")'   capital i, acute accent
   Case "Î"
      res.append("&Icirc;")'   capital i, circumflex accent
   Case "Ï"
      res.append("&Iuml;")'   capital i, umlaut mark
   Case "Ð"
      res.append("&ETH;")'   capital eth, Icelandic
   Case "Ñ"
      res.append("&Ntilde;")'   capital n, tilde
   Case "Ò"
      res.append("&Ograve;")'   capital o, grave accent
   Case "Ó"
      res.append("&Oacute;")'   capital o, acute accent
   Case "Ô"
      res.append("&Ocirc;")'   capital o, circumflex accent
   Case "Õ"
      res.append("&Otilde;")'   capital o, tilde
   Case "Ö"
      res.append("&Ouml;")'   capital o, umlaut mark
   Case "Ø"
      res.append("&Oslash;")'   capital o, slash
   Case "Ù"
      res.append("&Ugrave;")'   capital u, grave accent
   Case "Ú"
      res.append("&Uacute;")'   capital u, acute accent
   Case "Û"
      res.append("&Ucirc;")'   capital u, circumflex accent
   Case "Ü"
      res.append("&Uuml;")'   capital u, umlaut mark
   Case "Ý"
      res.append("&Yacute;")'   capital y, acute accent
   Case "Þ"
      res.append("&THORN;")'   capital THORN, Icelandic
   Case "ß"
      res.append("&szlig;")'   small sharp s, German
   Case "à"
      res.append("&agrave;")'   small a, grave accent
   Case "á"
      res.append("&aacute;")'   small a, acute accent
   Case "â"
      res.append("&acirc;")'   small a, circumflex accent
   Case "ã"
      res.append("&atilde;")'   small a, tilde
   Case "ä"
      res.append("&auml;")'   small a, umlaut mark
   Case "å"
      res.append("&aring;")'   small a, ring
   Case "æ"
      res.append("&aelig;")'   small ae
   Case "ç"
      res.append("&ccedil;")'   small c, cedilla
   Case "è"
      res.append("&egrave;")'   small e, grave accent
   Case "é"
      res.append("&eacute;")'   small e, acute accent
   Case "ê"
      res.append("&ecirc;")'   small e, circumflex accent
   Case "ë"
      res.append("&euml;")'   small e, umlaut mark
   Case "ì"
      res.append("&igrave;")'   small i, grave accent
   Case "í"
      res.append("&iacute;")'   small i, acute accent
   Case "î"
      res.append("&icirc;")'   small i, circumflex accent
   Case "ï"
      res.append("&iuml;")'   small i, umlaut mark
   Case "ð"
      res.append("&eth;")'   small eth, Icelandic
   Case "ñ"
      res.append("&ntilde;")'   small n, tilde
   Case "ò"
      res.append("&ograve;")'   small o, grave accent
   Case "ó"
      res.append("&oacute;")'   small o, acute accent
   Case "ô"
      res.append("&ocirc;")'   small o, circumflex accent
   Case "õ"
      res.append("&otilde;")'   small o, tilde
   Case "ö"
      res.append("&ouml;")'   small o, umlaut mark
   Case "ø"
      res.append("&oslash;")'   small o, slash
   Case "ù"
      res.append("&ugrave;")'   small u, grave accent
   Case "ú"
      res.append("&uacute;")'   small u, acute accent
   Case "û"
      res.append("&ucirc;")'   small u, circumflex accent
   Case "ü"
      res.append("&uuml;")'   small u, umlaut mark
   Case "ý"
      res.append("&yacute;")'   small y, acute accent
   Case "þ"
      res.append("&thorn;")'   small thorn, Icelandic
   Case "ÿ"
      res.append("&yuml;")'   small y, umlaut mark
   Case "€"
      res.append("&euro;")
   Case "™"
      res.append("&trade;")
   Case Else
      res.append(testc)
   End Select
Next
Return res.ToString
End Sub
 
Upvote 0
Top