ASP智能搜索的实现

2019-01-12 15:03:29于海丽

            End If
        End If
    %>
    在上面的代码中,有一个自定义函数 AutoKey ,该函数是实现智能搜索的核
心所在。代码如下:
    <%
    Function AutoKey(strKey)
        CONST lngSubKey=2
        Dim lngLenKey, strNew1, strNew2, i, strSubKey

        '检测字符串的合法性,若不合法则转到出错页。出错页你可以根据需要
进行设定。
        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   '若为空串,转到出错页
                Response.Redirect "error.htm"  
            Case 1   '若长度为1,则不设任何值
                strNew1=""
                strNew2=""
            Case Else  '若长度大于1,则从字符串首字符开始,循环取长度为
2的子字符串作为查询条件
                For i=1 To lngLenKey-(lngSubKey-1)
                    strSubKey=Mid(strKey,i,lngSubKey)
                    strNew1=strNew1 & " or U_Name like '%" & strSubKey