textarea内容的格式控制

textarea中能够用于格式控制的也就只有回车、换行和空格,如果不进行处理直接保存到数据库,显示在页面时这些格式将基本丢失。为了能将这些少得可怜的格式正确显示于页面上,通常我们得自定义一个函数对其传到服务器上的内容进行转换后再保存至数据库,函数如下:


Function HTMLEncode(ByVal reString)
    Dim Str:Str=reString
    If Not IsNull(Str) Then
           Str = Replace(Str, ">", ">")
        Str = Replace(Str, "<", "<")
        Str = Replace(Str, CHR(9), "    ")
        Str = Replace(Str, CHR(32), " ")
        Str = Replace(Str, CHR(39), "'")
        Str = Replace(Str, CHR(34), """)
        Str = Replace(Str, CHR(13), "")
        Str = Replace(Str, CHR(10), "<br/>")
        HTMLEncode = Str
    End If
End Function


这样转换后在页面中总算能正常显示了,可是当要修改再将内容读取到textarea时,你会发现多了好多的<br/>、 等字符,及不利于再次编辑,所以我们得再写一个和上面那个函数作用相反的函数来解决问题:


Function HTMLDecode(ByVal reString)
    Dim Str:Str=reString
    If Not IsNull(Str) Then
        Str = Replace(Str, ">", ">")
        Str = Replace(Str, "<", "<")
        Str = Replace(Str, "    ", CHR(9))
        Str = Replace(Str, " ", CHR(32))
        Str = Replace(Str, "'", CHR(39))
        Str = Replace(Str, """, CHR(34))
        Str = Replace(Str, "", CHR(13))
        Str = Replace(Str, "<br/>", CHR(10))
        HTMLDecode = Str
    End If
End Function


OK,这下可以保证添加、修改和显示时都是一个样子了!^_^

评论: 1 | 引用: 0 | 查看次数: 4721
发表评论
登录后再发表评论!