asp搜索结果的显示

<!--实现搜索结果的关键词变色标注的程序
 四月 5th, 2006
在搜索得到的文本中,从第一个关键词出现的前50个字开始显示,把关键词替换为红色,这比单纯的用replace得到的显示结果更人性化一些,因为用replace的话一旦关键词出现在文本的后面,就可能出现在结果页面显示的文本简要里不能出现关键词。虽然是很老的代码了,不过对于现在的搜索脚本的编写依然有参考作用。-->
<%

'以前写全文检索程序的时候写的.

'原创 by 飞鸟@dev-club.com

'Email: flybird@dev-club.com

'ie5.5 脚本引擎 required

   dim patern

   dim found

   dim str

   dim result

   patern="(a)|(b)"

   str=" A dog fall in love with a cat. Can you believe?"

   result=""  

   call getMatchText(str,result,false)

   Response.Write result

   sub getMatchText(byref str,byref result,isNeedTrunc)

     on error resume next

     Dim regEx, Match, Matches

     dim tStr

     Set regEx = New RegExp     '建立正则表达式。    

     regEx.Pattern = (patern)     '设置模式。

     regEx.IgnoreCase = True    '设置是否区分字符大小写。

     regEx.Global = True        '设置全局可用性。

     Set Matches = regEx.Execute(str)  '执行搜索。  

     if err.number<>0 then

       response.write "错误1:" & err.description

       err.clear

       exit sub

     end if

     if matches.count <>0 then

       dim startIndex      

       dim myMatchValue

       startIndex=1

       for each match in matches

         if (instr(str,match.value)>0) then

           if instr(str,match.value)-50 >0 then

             startIndex=instr(str,match.value)-50

           else

             startIndex=1

           end if

           myMatchValue=match.value

           exit for

         end if

       next

       if isNeedTrunc then

         result= (mid(str,startIndex,strLength(myMatchValue)+100))

       else

         result= (str)  

       end if

       for each match in matches

         if not(instr(result,"<font color=red>" & match.value & "</font>")>0) then

           result=replace(result,match.value,"<font color=red>" & match.value & "</font>" )

         end if

       next

       found=true

     else

       found=false

     end if  

     set regEx=nothing

   end sub

%>

 http://lyp.cn/study/code/asp/2006-04-05/260/full-text-search-keyword-replace.html
------------------------------------------------------------------------------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值