如何使ASP實(shí)現(xiàn)搜索關(guān)鍵字高亮(2)_ASP教程
教程Tag:暫無Tag,歡迎添加,賺取U幣!
推薦:解析ASP檢查網(wǎng)站鏈接是否正常的函數(shù)% FunctionurlChk(sUrl) onerrorresumenext SetxmlHttp=Server.CreateObject(Microsoft.XMLHTTP) xmlHttp.openGET,sUrl,false xmlHttp.send ifxmlHttp.Status200then urlChk=false else urlChk=true endif EndFunction sUrl=http://www.code-123.com ifu
在上面的代碼中,有一個(gè)自定義函數(shù) AutoKey ,該函數(shù)是實(shí)現(xiàn)智能搜索的核心所在。代碼如下:| <% Function AutoKey(strKey) CONST lngSubKey=2 Dim lngLenKey, strNew1, strNew2, i, strSubKey ’檢測字符串的合法性,若不合法則轉(zhuǎn)到出錯(cuò)頁。出錯(cuò)頁你可以根據(jù)需要進(jìn)行設(shè)定。 if InStr(strKey,"=")<>0 or InStr(strKey,"`")<>0 or InStr(strKey,"")<>0 or InStr(strKey," ")<>0 or InStr(strKey," ")<>0 or InStr(strKey,"")<>0 or InStr(strKey,chr(34))<>0 or InStr(strKey,"\")<>0 or InStr(strKey,",")<>0 or InStr(strKey,"<")<>0 or InStr(strKey,">")<>0 then Response.Redirect "error.htm" End If lngLenKey=Len(strKey) Select Case lngLenKey Case 0 ’若為空串,轉(zhuǎn)到出錯(cuò)頁 Response.Redirect "error.htm" Case 1 ’若長度為1,則不設(shè)任何值 strNew1="" strNew2="" ’Case Else 若長度大于1,則從字符串首字符開始,循環(huán)取長度為2的子字符串作為查詢條件 For i=1 To lngLenKey-(lngSubKey-1) strSubKey=Mid(strKey,i,lngSubKey) strNew1=strNew1 & " or U_Name like %" & strSubKey & "%" strNew2=strNew2 & " or U_Info like %" & strSubKey & "%" Next End Select ’得到完整的SQL語句 AutoKey="Select * from T_Sample where U_Name like %" & strKey & "% or U_Info like %" & strKey & "%" & strNew1 & strNew2 End Function %> |
要實(shí)現(xiàn)智能搜索,其核心就是將搜索關(guān)鍵字進(jìn)行自動(dòng)分組。在此處,我們使用了循環(huán)取長度為2的子串的方法。為什么不將子串長度定為1、3、4或其他呢?這是因?yàn)槿糇哟L度小于2即為1時(shí),會(huì)失去將關(guān)鍵字分組的功能,而若子串長度大于2,則會(huì)丟失一些詞組。大家可以將 CONST lngSubKey=2改為其他數(shù)字試一試,孰優(yōu)孰劣自見分曉。
最后,別忘了將數(shù)據(jù)連接關(guān)閉,以釋放資源。
| <% CNN.Close Set CNN=Nothing %> |
至此,這個(gè)智能搜索引擎已經(jīng)完成了。你還可以將其繼續(xù)完善,比如添加分頁、突出顯示等功能。
分享:ASP教程之a(chǎn)sp制作常見flash新聞圖片輪換代碼這是一個(gè)正常的網(wǎng)頁文件 html head metahttp-equiv=Content-Typecontent=text/html;charset=gb2312/ title 新聞圖片輪換 /title /head body !--#Includefile=diaoyong.asp-- /body /html 任何網(wǎng)頁只要調(diào)用下面這個(gè)文件(diaoyong.asp)就可以了,所用f
相關(guān)ASP教程:
- asp FSO 讀寫文件本文件實(shí)現(xiàn)代碼
- asp中isNull、isEmpty和空字符串的區(qū)別
- asp獲取用戶真實(shí)IP地址的方法
- asp連接sqlserver數(shù)據(jù)庫實(shí)現(xiàn)代碼
- asp中正則表達(dá)式過濾html代碼函數(shù)
- asp中g(shù)et post提交表單區(qū)別
- 網(wǎng)頁模板:ASP內(nèi)建對象Request
- xmlhttp的open方法使用詳解
- ASP的常用的自定義函數(shù)大全
- asp中用for循環(huán)的一個(gè)小技巧
- eWebEditor v3.8 列目錄
- ASP無組件分頁實(shí)現(xiàn)思路及代碼
- 相關(guān)鏈接:
- 教程說明:
ASP教程-如何使ASP實(shí)現(xiàn)搜索關(guān)鍵字高亮(2)
。