网络编程
位置:首页>> 网络编程>> Asp编程>> 如何使数据库中取出的数据保持原有格式(2)

如何使数据库中取出的数据保持原有格式(2)

作者:cnbruce 来源:cnbruce博客 发布时间:2008-11-27 16:16:00 

标签:数据库,Replace,替换,字符串,asp

3,练习使用request对象接受数据,并且综合运用些函数。

cnbruce.html

<form action="cnbruce.asp" method="post">
<input type="text" name="title"><br>
<textarea name="content" rows=10 cols=20></textarea><br>
<input type="submit">
</form>

cnbruce.asp

<%
function th(str)
str=replace(str,"妈的","MD")
str=replace(str,"靠","KAO")
th=str
end function

function encode(str)
str = replace(str, ">", "&gt;")
str = replace(str, "<", "&lt;")
str = Replace(str, CHR(32), "&nbsp;")
str = Replace(str, CHR(13), "")
str = Replace(str, CHR(10) & CHR(10), "</P><P>")
str = Replace(str, CHR(10), "<BR>")
encode=str
end function
%>

<%title=request.form("title")
content=request.form("content")%>

文章标题:<%=th(title)%><hr>
文章内容:<%=encode(th(content))%>

th(str)为自定义函数,主要功能很简单:过滤字符。encode(str)也为自定义函数,主要功能是完整地显示被传递的信息。

CHR(10)表示换行,CHR(13)表示回车,CHR(32)表示空格。

附加功能:显示UBB代码。

即ubb.asp中含有ubb()函数。

增强的cnbruce.asp

<!--#include file="http://www.cnbruce.com/blog/ubb.asp"-->
<%
function th(str)
str=replace(str,"妈的","MD")
str=replace(str,"靠","KAO")
th=str
end function
%>

<script>
function runEx(cod1) {
cod=document.all(cod1)
var code=cod.value;
if (code!=""){
var newwin=window.open('','',''); 
newwin.opener = null 
newwin.document.write(code); 
newwin.document.close();
}
}
</script>

<%title=request.form("title")
content=request.form("content")%>

文章标题:<%=th(title)%><hr>
文章内容:<%=ubb(unhtml(th(content)))%>

有了这些锻炼,相信在使用数据库的时候同样可以胜任。

Replace函数的作用:返回字符串,其中指定数目的某子字符串被替换为另一个子字符串。

Replace(expression, find, replacewith[, compare[, count[, start]]]) 

expression 必选项。字符串表达式包含要替代的子字符串。
find 必选项。被搜索的子字符串。
replacewith 必选项。用于替换的子字符串。
start 可选项。expression中开始搜索子字符串的位置。如果省略,默认值为1。在和count关联时必须用.
count 可选项。执行子字符串替换的数目。如果省略,默认值为-1,表示进行所有可能的替换。在和start关联时必须用。
compare 可选项。指示在计算子字符串时使用的比较类型的数值。有关数值,请参阅"设置"部分。如果省略,缺省值为0 ,这意味着必须进行二进制比较。

从前到后分别是"在哪里找,找什么,用什么替换"。

在VBScript里""(两个引号)在字符串里会自动存为一个引号,在JavaScript里\"等于引号

vbcrlf相当于回车符和换行符的组合.

至于Replace函数,其作用就是将字符串Str中的vbcrlf替换为"<Br>" ,vbCrLf&vbCrLf替换为"<p></p>"

下面的一个就是很有用的文字转换程序



 

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com