textarea 回车换行空格等字符存入数据库并读取

 做简单留言或评论时经常会遇到在textarea里用回车和空格做的排版,提交后格式没有保留,这是因为textarea里输入和车出现的是编码vbCrlf,页面读取数据库显示的内容必须有<br/>才可以显示换行,所以我们要做的就是在存入数据时把vbCrlf换成<br/>。

录入数据库时

str = Replace(str,vbCrlf, "<br/>" ) 

这样页显示的时候就有换行了

当要编辑信息时

str = Replace(str," <br/>" ,vbCrlf)

这样放到编辑的textarea上时就有换行了

写到数据库的内容,读出来时没有了空格和换行,类似问题的解答就是在写入数据库做Replace替换

<%
'----------Html转换为文本:编辑时拿来做转换
Function Html2Text(Str1)
If isNULL(Str1) Then
Html2Text=""
Exit Function
End If
Str1=Replace(Str1,"<"," <")
Str1=Replace(Str1,">",">")
Str1=Replace(Str1," <br>",VBcrlf)
Str1=Replace(Str1,""",chr(34))
Str1=Replace(Str1," "," ")
Str1=Replace(Str1,"&","&")
Html2Text=Str1
End Function
'-------------------文本转换为Html:写入数据库时使用---------------
Function Text2Html(Str1)
If isNULL(Str1) Then
Text2Html=""
Exit Function
End If
Str1=Replace(Str1,"&","&")
Str1=Replace(Str1," <","<")
Str1=Replace(Str1,">",">")
Str1=Replace(Str1,VBcrlf," <br>")
Str1=Replace(Str1,chr(34),""")
Str1=Replace(Str1,chr(9),"   ")
Str1=Replace(Str1," "," ")
Text2Html=Str1
End Function
%>

输出时如果不是在文本框就不用text2html

posted @ 2010-04-27 10:46  anny0320  阅读(9794)  评论(0编辑  收藏  举报