Encode to HTML

Thought I would share just in case anyone else needs to output characters to a web browser.

[code]Function encodeToHTML(cellText as String) As string
dim result as string = “”
dim charsOk as string = chr(34) + " .!$#@±/\|~’;:[]{}=_()*`0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
dim currentChar as string = “”

for lp as integer = 1 to len(cellText)
currentChar = Mid(cellText, lp, 1)

if InStr(charsOk, currentChar) > 0 then
  result = result + currentChar
  result = result + "&#" + cstr(Asc(currentChar)) + ";"
end if


return result
End Function[/code]

For a long string, this would be slow. If I may, use arrays instead.

(FYI, retrieving Len on a long string, especially one that has a lot of Unicode in it takes a surprisingly long time so you don’t want to call it repeatedly in a loop. LenB doesn’t suffer from that problem, but is also not appropriate in most cases.)

Function encodeToHTML(cellText As String) As String
  dim result() as string
  static charsOk as string = _
  chr(34) + _
  " .!$#@+-/\\|~';:[]{}=_()*`0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  dim currentChar as string = ""
  cellText = cellText.ConvertEncoding( Encodings.UTF8 )
  dim chars() as string = cellText.Split( "" )
  redim result( chars.Ubound )
  for lp as integer = 0 to chars.Ubound
    currentChar = chars( lp )
    if InStr(charsOk, currentChar) <> 0 then
      result( lp ) = currentChar
      result( lp ) = "&#" + cstr(Asc(currentChar)) + ";"
    end if
  return join( result, "" )
End Function

Thank you so much for taking the time to show me a better way of doing it. The feedback is really helpful and a totally different approach which I wouldnt have done and I can see how I can use the way you have done this with other things I do. I didnt know that about the Len and LenB. That has been really helpful.

The next step is to get really fancy and replace things like “&” with “&”. :slight_smile:

if you need speed: EncodingToHTMLMBS function.

Well, naturally. :slight_smile:

Why “Encoding…” and not “Encode…”?

bad english.

and difficult to change.

Either way it is much quicker than what I did, especially with very large text blocks, thanks everyone.

[quote=96510:@Christian Schmitz]bad english.

and difficult to change.[/quote]

Not that it’s really important, but couldn’t you deprecate the current methods and add the “correctly” named ones?

I know this will sound stupid but I did look in the list to see if Xojo etc had a function built in and I use your MBS suite but didnt see your function because I was looking for Encode and not Encoding so skipped past it so others may do the same so I would give +1 to the idea of it becoming EncodeToHTMLMBS.