In some cases, we cannot control the appearance of garbled characters. For example, when sending emails, some emails are garbled. For example, Ajax data is always garbled. What should we do? A few days ago, I encountered a situation where I submitted a form using Ajax and sent an email. So I wrote the following two functions to solve these two problems.
The first function converts the data in HTML into HTML entities, while the HTML Tag is automatically not transferred, so that no garbled code is found anywhere. You can choose to send an HTML-format email when sending an email.
The second function replaces JavaScript data with escape characters, which also avoids conversion of JS keywords and other characters. No matter what encoding the webpage is, it will not be garbled. Let's talk about the following code.
The Code is as follows:
Function htmlentities (str)
Dim a, I, char
For I = 1 to Len (str)
Char = mid (str, I, 1)
A = Ascw (char)
If a> 128 Or a <0 then
Htmlentities = htmlentities & "& #" & clng ("& h" & hex (Ascw (char) & ";"
Else
Htmlentities = htmlentities & char
End if
Next
End Function
Function Unicode (str1)
Dim str, temp
Str = ""
For I = 1 To Len (str1)
Temp = Hex (AscW (Mid (str1, I, 1 )))
If len (temp) <5 Then temp = Right ("0000" & temp, 4)
Str = str & "u" & temp
Next
Unicode = str
End Function
If you use it with your own attitude, you can use anything directly. Of course, many times we will rewrite some code to meet the needs of special situations, if you report a learning and research attitude, you should note that the Ascw (char) function may return a long integer value, while ASP regards it as an integer, A small amount of processing is required.