几年前我使用它构建了一个应用程序;我所做的是前端为您提供了一个文本框来输入您的条款,它有一个下拉菜单可以在以下几个选项之间进行选择:
<option selected value="AND">All of the words entered</option>
<option value="OR">Any of the words entered</option>
<option value="ALL">The exact phrase entered</option>
所以我不希望他们输入 ands 或 ors - 我为他们这样做。我确实让他们引用一个副词。
下面是一些完成此操作的代码。生成的短语作为参数发送到过程;正如上面提到的评论者所说,您确实必须这样做以防止 SQL 注入。不幸的是,这段代码是遗留的 ASP VBScript,甚至没有按照该语言的标准编写,但也许这给了你一个想法。
function formatContainsString (sCriteria, sANDorOR)
dim sReturnBuf 'where we build our string
dim sWordList 'sCriteria split
dim lCurPos 'where we are at in sWordList
dim bInnerQuotes ' an open quote was found
if sCriteria = "" or sANDorOR = "" then
formatContainsString = ""
exit function
end if
' If the passed parameter is 'ALL' then use the exact phrase typed by users
if sANDorOR = "ALL" then
formatContainsString = "'" & chr(34) & sCriteria & chr(34) & "'"
Exit Function
End If
sReturnBuf = "'"
sWordList = split(sCriteria," ")
for lCurPos = 0 to ubound(sWordList)
if bInnerQuotes then 'want to pass this as a single phrase
sReturnBuf = sReturnBuf & " " & sWordList(lCurPos)
if right(sWordList(lCurPos),1) = chr(34) then
sReturnBuf = left(sReturnBuf,len(sReturnBuf)-1) & "*" & chr(34)
sReturnBuf = sReturnBuf & " " & sANDorOR & " "'phrase is over
bInnerQuotes = False
end if
else
if left(sWordList(lCurPos),1) = chr(34) then
sReturnBuf = sReturnBuf & sWordList(lCurPos)
bInnerQuotes = True
else
sReturnBuf = sReturnBuf & chr(34) & sWordList(lCurPos) & "*" & _
chr(34) & " " & sANDorOR & " "'quote the word
end if
end if
next
'finally, remove the last AND or OR... and append the tick
formatContainsString = left(sReturnBuf,len(sReturnBuf)-(len(sANDorOR)+1)) _
& "'"
end function