关于ASP自动给文章中的关键词加链接的问题
'关键词带连接 Function LinkWord(word) Dim KeyWord,Kurl,RstK,SqlK,MiddleWord,StartWord,LastWord,FinalWord,P SqlK="SELECT keyword_word,keyword_url FROM Lelv_w_KeyWord WHERE keyword_status=1" Set RstK=Server.CreateObject("Adodb.Recordset")'Conn.Execute(SqlK) RstK.open SqlK,conn,1,1 'response.write("<script language=javascript>alert('"&RstK.recordcount&"');</=script>") 'Do While Not RstK.Eof IF Not(RstK.bof and RstK.eof) then '不为空 For P=1 to RstK.Recordcount '循环替换 KeyWord=RstK("keyword_word") Kurl=RstK("keyword_url") If RstK.Recordcount=1 then StartWord=Replace(""&word&"",""&KeyWord&"","<a href="""&Kurl&""">"&KeyWord&"</a>"[,0[,1[,0]]]) FinalWord=StartWord Exit For '如果记录只有1条 StartWord=Replace(""&word&"",""&KeyWord&"","<a href="""&Kurl&""">"&KeyWord&"</a>",1,1) Else '如果记录不止一条 If p=1 then '在第一条 StartWord=Replace(""&word&"",""&KeyWord&"","<a href="""&Kurl&""">"&KeyWord&"</a>") 'response.Write("<script language=javascript>alert('"&FinalWord&"');<script>") MiddleWord=StartWord'传递 response.Write("<script language=javascript>alert('"&MiddleWord&"');</script>") else '不在第一条 LastWord=Replace(""&MiddleWord&"",""&KeyWord&"","<a href='"&Kurl&"'>"&KeyWord&"</a>") FinalWord=LastWord'传递 end if end if RstK.MoveNext Next Response.Write(""&StartWord&"") End if RstK.close Set RstK=nothing End Function
用字符串截取
if instr(str,key)>0 then
'存在,第一个出现关键字的置:instr(str,key)再往后截取关键字的长度,再替换。组成新的str.再找下一个关键字。
else
'不存在,下一个关键字
end if