asp的站内搜索功能

本文介绍了一个简单的ASP脚本,用于实现网站的全文检索功能。该脚本能递归地搜索当前目录及其子目录下所有的HTML文件,并查找指定关键词。适用于小规模网站。
下面代码可以全部复制粘贴到一个search.asp文件中,就能够实现静态页面的全文检索功能了。适合于页面数较少的站点,估计超过100个页面性能就很差了。

None.gif<%
None.gifHead 
= "站内搜索"
None.gifSearchString 
= Request("SearchString")
None.gifcount
=0
None.gif
None.gif
'把当前目录的实际路径转换为虚拟路径
None.gif
Function UnMapPath( Path )
None.gif    UnMapPath 
= Replace(Mid(Path, Len(Server.MapPath("/")) + 1), """/")
None.gif
End Function
None.gif
None.gif
None.gif
Function SearchFile( f, s, title )
None.gif   
Set fo = fs.OpenTextFile(f)
None.gif   content 
= fo.ReadAll
None.gif   fo.Close
None.gif   SearchFile 
= InStr(1, content, S, vbTextCompare) > 0 
None.gif   
If SearchFile Then
None.gif      pos1 
= InStr(1, content, "<TITLE>", vbTextCompare)
None.gif      pos2 
= InStr(1, content, "</TITLE>", vbTextCompare)
None.gif      title 
= ""
None.gif      
If pos1 > 0 And pos2 > 0 Then
None.gif         title 
= Mid( content, pos1 + 7, pos2 - pos1 - 7 )
None.gif      
End If
None.gif   
End If
None.gif
End Function
None.gif
None.gif
Function FileLink( f, title )
None.gif   vPath 
= UnMapPath( f.Path )
None.gif   
If title = "" Then title = f.Name
None.gif   FileLink 
= "<A HREF=""" &  vPath & """>" & title & "</A>"
None.gif   FileLink 
= "<UL>·" & FileLink & "</UL>"
None.gif
End Function
None.gif
None.gif
Sub SearchFolder( fd, s ) 
None.gif   found 
= False  
None.gif   
For each f In fd.Files
None.gif      pos 
= InStrRev(f.Path, "." )
None.gif      
If pos > 0 Then
None.gif         ext 
= Mid(f.Path, pos + 1 )
None.gif      
Else
None.gif         ext 
= ""
None.gif      
End If
None.gif      
If LCase(ext) = "htm" Then
None.gif         
If SearchFile( f, s, title ) Then
None.gif            Response.Write FileLink(f, title)
None.gif            count
=count+1
None.gif            
' Response.Write cstr(count)
None.gif
         End If
None.gif      
End If
None.gif   
Next
None.gif
None.gif   
For each sfd In fd.SubFolders
None.gif      SearchFolder sfd, s
None.gif   
Next
None.gif
End Sub
None.gif%
>
None.gif
<html>
None.gif
None.gif
<head>
None.gif
<meta http-equiv="Content-Type" content="text/html; charset=gb_2312-80">
None.gif
<title><%=Head%></title>
None.gif
</head>
None.gif
None.gif
<body bgcolor="#FFFFFF">
None.gif
None.gif
<h1><%=Head%></h1>
None.gif
None.gif
<hr>
None.gif
None.gif
<!-- 注意search.asp为本文件,可根据需要修改!-->
None.gif
None.gif
<form action="search.asp" method="Get"> 
None.gif    
<p>请输入欲搜索的内容: <input type="text"
None.gif    size
="20" name="SearchString" value="<%=SearchString%>"> <input
None.gif    type
="submit" value="搜索"> </p>
None.gif
</form>
None.gif
<%
None.gif
Set fs = Server.CreateObject("Scripting.FileSystemObject")
None.gif
Set fd = fs.GetFolder( Server.MapPath("./") )   '设置开始搜索的路径(将遍历其所有子目录,当前设置为search.asp所在目录)!
None.gif

None.gif
If SearchString <> "" Then
None.gif   Response.Write 
"<H2>搜索<font color=red>" & SearchString & "</font>结果如下:</H2><P>"
None.gif   SearchFolder fd,SearchString
None.gif
End If
None.gif%
>
None.gif
<hr>
None.gif
</body>
None.gif
</html>
None.gif
None.gif
None.gif

转载于:https://www.cnblogs.com/qiangsheng/archive/2007/07/01/801806.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值