|
蓝森林 http://www.lslnet.com 2006年8月25日 8:28
[asp]关于replace()
[code]
<%
db_path="database/wzdqy.mdb"
set conn=server.createobject("adodb.connection")
connstr="provider=microsoft.jet.oledb.4.0;data source="&server.mappath(db_path)
conn.open connstr
set rs=server.createobject("adodb.recordset")
sql="select* from article order by id desc"
rs.open sql,conn,1,1
%>
<%
function encode(str)
str = Replace(str, CHR(32), " ")
str = Replace(str, CHR(13), "")
str = Replace(str, CHR(10) & CHR(10), "</P><P>")
str = Replace(str, CHR(10), "<BR>")
encode=str
end function
%>
<table border=1>
<%if rs.eof and rs.bof then
response.write("暂时没有文章")
else
do until rs.eof
%>
<tr><td><%="文章的标题是:"& rs("title")%></td></tr>
<tr><td><%="文章的作者是:"& rs("author")%></td></tr>
<tr><td><%="文章加入的时间是:"& rs("time")%></td></tr>
<tr><td><%="文章的内容是:<p>"& encode(rs("content"))%></td></tr>
<%
rs.movenext
loop
end if%>
</table>
<%rs.close()
set rs=nothing
conn.close()
set conn=nothing
%>
[/code]
它给我报错:
错误类型:
Microsoft VBScript 运行时错误 (0x800A005E)
无效使用 Null: 'replace'
/newasp/showit.asp, 第 14 行
想要替换文章中的空格,回车。。。,上面的代码应该怎么写啊?请高手指点下,先谢了! |
function encode(str)
[color=red]
if not isnull(str) then
[/color]
str = Replace(str, CHR(32), " ")
str = Replace(str, CHR(13), "")
str = Replace(str, CHR(10) & CHR(10), "</P><P>")
str = Replace(str, CHR(10), "<BR>")
encode=str
[color=red]
else
exit function
end if
[/color]
end function
加上红色的代码即可 |
嘿,解决了。谢了!
可是,为什么呢?
是代码不严谨吗? |
原因是 replace() 不能对空值进行替换,所以会出错..
if not isnull(str) then '不为空时才进行替换..
|
这么快就有回复!
楼上,谢了!
还有什么是需要替换的,高手能不能写个全点的replace()代码?
我好为下面的那段代码添加上去?
function encode(str)
if not isnull(str) then
str = Replace(str, CHR(32), " ")
str = Replace(str, CHR(13), "")
str = Replace(str, CHR(10) & CHR(10), "</P><P>")
str = Replace(str, CHR(10), "<BR>")
encode=str
else
exit function
end if
end function |
|