用ASP做一个TOP COOL的站内搜索

作者:网络 来源:佚名 更新时间:2008-02-05 18:22:42 点击:



该搜索引擎由一个htm文件一个asp文件组成,主要是运用filesystemobject组件来达到目的,功能强大,修改界面以后可以直接拿来使用,当然加上一点自己的东西就更加好了。

searchpage.htm
该htm文件用来传入条件

<html>
<head>
<title>asp搜索引擎范例</title>
</head>
<body>
<center>
<form method=post action="search.asp">

<table bgcolor="#cc6633" border="0">
<tr>

<td rowspan="3" bgcolor="#cc6633" width="21" nowrap> </td>

<td width="363" nowrap> <font color="#ffffff">
<input type="text" name="searchtext" size="22">
<input type="checkbox" name="case">
高度敏感</font> </td>

<td rowspan="3" width="10">
<input type="submit" value="确定">
<p>

<input type="reset" value="清除">
</td>

<td rowspan="3" bgcolor="#cc6633" width="28"> </td>
</tr>
<tr>

<td width="363" nowrap> <font color="#ffffff"> 返回结果
<select name="rlength" >
<option value="200" selected>长信息 </option>
<option value="100">短信息</option>
<option value="0">只返回连接</option>
</select>
<select name="rresults">
<option value="10" selected>10
<option value="25">25
<option value="50">50
</select>
</font>
</td>
</tr>
<tr>

<td width="363" nowrap> <font color="#ffffff"> 必须包括:
<input type="checkbox" name="iimage">
图片
<input type="checkbox" name="izips">
zip格式
<input type="checkbox" name="ijavas"> javascript
</font>
</td>
</tr>
</table>
</form>
</center>

<dl>
<dd> </dd>
</dl>
</body>
</html>

------------------------------------------------------------------------
显示结果的asp程序
search.asp

<html>
<head>
<title>'<%=request("searchtext")%>'的搜索结果</title>
</head>
<body>
<b>'<%=request("searchtext")%>'的搜索结果</b><br>
<%
const fsoforreading = 1
dim objfile, objfolder, objsubfolder, objtextstream
dim bolcase, bolfilefound, boltagfound
dim strcount, strdetag, strext, strfile, strcontent, strroot, strtag, strtext, strtitle, strtitlel
dim reqimage, reqjavas, reqlength, reqnumber, reqzip

strfile = ".asp .htm .html .js .txt .css"
strroot = "/"
strtext = request("searchtext")
strtag = chr(37) & chr(62)
bolfilefound = false
boltagfound = false
if request("case") = "on" then bolcase = 0 else bolcase = 1
if request("iimage") = "on" then reqimage = "<img" else reqimage = ""
if request("izips") = "on" then reqzips = ".zip" else reqimage = ""
if request("ijavas") = "on" then reqjavas = "javascript" else reqimage = ""
if request("rresults") = "10" then reqnumber = 10
if request("rresults") = "25" then reqnumber = 25
if request("rresults") = "50" then reqnumber = 50
reqlength = request("rlength")

set objfso = server.createobject("scripting.filesystemobject")
set objfolder = objfso.getfolder(server.mappath(strroot))

schsubfol(objfolder)

sub schsubfol(objfolder)
for each objfile in objfolder.files
if strcount + 1 > reqnumber or strtext = "" then exit sub
if response.isclientconnected then
set objtextstream = objfso.opentextfile(objfile.path,fsoforreading)

strcontent = objtextstream.readall

if instr(1, strcontent, strtag, bolcase) then
else
if mid(objfile.name, len(objfile.name) - 1, 1) = "." then strext = mid(objfile.name, len(objfile.name) - 1, 2)
if mid(objfile.name, len(objfile.name) - 2, 1) = "." then strext = mid(objfile.name, len(objfile.name) - 2, 3)
if mid(objfile.name, len(objfile.name) - 3, 1) = "." then strext = mid(objfile.name, len(objfile.name) - 3, 4)
if mid(objfile.name, len(objfile.name) - 4, 1) = "." then strext = mid(objfile.name, len(objfile.name) - 4, 5)
if instr(1, strcontent, strtext, bolcase) and _
instr(1, strcontent, reqimage, 1) and _
instr(1, strcontent, reqzips, 1) and _
instr(1, strcontent, reqjavas, 1) and _
instr(1, strfile, strext, 1) then
if instr(1, strcontent, "<title>", 1) then strtitle = mid(strcontent, instr(1, strcontent, "<title>", 1) + 7, instr(1, strcontent, "</title>", 1)) else strtitle = "未命名"

strcount = strcount + 1
response.write "<dl><dt><b><i>"& strcount &"</i></b> - <a href=" & objfile.path & ">" & strtitle & "</a></a></dt><br><dd>"
strtitlel = instr(1, strcontent, "</title>", 1) - instr(1, strcontent, "<title>", 1) + 7

strdetag = ""
boltagfound = false

do while instr(strcontent, "<")
boltagfound = true
strdetag = strdetag & " " & left(strcontent, instr(strcontent, "<") - 1)
strcontent = mid(strcontent, instr(strcontent, ">") + 1)
loop

strdetag = strdetag & strcontent
if not boltagfound then strdetag = strcontent

if reqlength = "0" then response.write objfile.path & "</dd></dl>" else response.write mid(strdetag, strtitlel, reqlength) & "...<br><b><font size='2'>url: " & objfile.path & " - 上次修改时间: " & objfile.datelastmodified & " - " & formatnumber(objfile.size / 1024) & "kbytes</font></b></dd></dl>"
bolfilefound = true
end if
objtextstream.close
end if
end if
next
end sub

for each objsubfolder in objfolder.subfolders
schsubfol(objsubfolder)
next

if not bolfilefound then response.write "没有匹配结果"
if bolfilefound then response.write "<b>搜索结束</b>"

set objtextstream = nothing
set objfolder = nothing
set objfso = nothing
%>
</body></html>