asp 多个关键字 变红色

<%
searchdata=request("Key") '从文本框中获得输入的关键字
if trim(searchdata)="" then '如果没有关键字的输入或者只输入的空格,警告!
response.write "<script>alert('请输入查询搜索关键字!');history.back();</script>"
response.end
end if

searchdata=replace(searchdata,"'","’")'将获得的字符串中英文单引号换成中文单引号
searchdata=replace(searchdata,","," ")
searchdata=replace(searchdata,"|"," ")
searchdata=replace(searchdata,"+"," ")
searchdata=replace(searchdata,","," ")
searchdata=replace(searchdata,","," ")

searchdata=Rtrim(LTrim(searchdata))'去掉获得的字符串最左边空格和最右边空格
sql1="" '网站关键字匹配sql语句
sql2="" '网站标题匹配sql语句
sql3="" '网站简要说明匹配sql语句

searchdatatmp=split(searchdata," ") '将输入的字符串根据空格分开,获得一个数组
max=ubound(searchdatatmp) '得出这个数组的维数,即输入的关键字个数

if max=0 then '如果max等于0说明只输入了一个关键字,那么就不需要循环处理
sql1=sql1&" title like '%" & searchdatatmp(i) & "%' " '网站关键字模糊搜索
sql2=sql2&" content like '%" & searchdatatmp(i) & "%' " '网站标题模糊搜索
sql3=sql3&" add_time like '%" & searchdatatmp(i) & "%' " '网站简要说明搜索
else '如果含有多个关键字,采用循环处理sql语句
for i=0 to max '如果关键字很多,我们要求每一个搜索都要匹配每一个关键字,通过循环来实现
if i=0 then '写入下面sql语句作为开头,仔细研究一下下面的代码
sql1=sql1&" (title like '%" & searchdatatmp(i) & "%' and "
sql2=sql2&" (content like '%" & searchdatatmp(i) & "%' and "
sql3=sql3&" (add_time like '%" & searchdatatmp(i) & "%' and "
else
if i=max then '如果循环到最后一个关键字,写入下面sql语句作为结尾
sql1=sql1&" title like '%" & searchdatatmp(i) & "%') "
sql2=sql2&" content like '%" & searchdatatmp(i) & "%') "
sql3=sql3&" add_time like '%" & searchdatatmp(i) & "%') "
else '如果关键自己不是开头的也不是结尾的,那么循坏写入下面的sql语句
sql1=sql1&" title like '%" & searchdatatmp(i) & "%' and "
sql2=sql2&" content like '%" & searchdatatmp(i) & "%' and "
sql3=sql3&" add_time like '%" & searchdatatmp(i) & "%' and "
end if
end if
next '循环结束
end if

sql="select * from diary where "&sql1&" or "&sql2&" or "&sql3
set rsd=server.createobject("ADODB.Recordset")
rsd.open sql,conn,1,3
if rsd.eof or rsd.bof then
response.write("<script> alert('没有包含关键字的纪录');history.back();</script>")
response.end
else
do while not rsd.eof

%>

<%=rsd("title")%>

<%

rsd.movenext
loop
end if
rsd.close
set rsd=nothing
%>
代码如上,请高手帮忙修改一下!,楼下抄的真快,:》
最新回答
白色季节

2025-03-27 03:22:18

在显示字段用使用Replace函数:
<%= Replace(ind("title"),title,"<span class='style1'>"&title&"</span>") %>

例子: <%=Replace("ABCD123ABC","AB","ab")%>
ABCD123ABC 是查出来的字串
AB 是要替换的字串
ab 是替换成的字串
结果: abCD123abC
要显示红色就是把字段中的关键字加上class
脸红妹妹

2025-03-27 01:46:25

显示内容前
先用replace函数把所有的关键字的代码加 HTML的红色
标签就行了
岁月和你两无言

2025-03-27 04:15:32

构造sql语句方法1:
key="1+2+3"
sql=" 1=1"
if instr(key,"+")<>0 then
str = split(key,"+")
for i=0 to ubound(str)
sql = sql&" and 字段 like'%"&str(i)&"%'"
next
end if

sql1="select * from table where "&sql

方法2:
key="1+2+3"
key=replace(replace(key,"+",",")," ",",")

sql="select * from table where 字段 in('"&key&"')"

至于颜色可以放到前台用js替换下关键字就行