精彩专题推荐:建站之入门课 建站之必修课 建站之关键课 网站价值所在 流量提高专题 css+div 标准 个人网站打造全过程
返回建站学首页
导航:
建站首页 | 网站设计 | 网站开发 | 网站运营 | 网页软件 | 建站指南 | 搜索优化 | 图像处理 | 视频教程 | 书籍教程 | 建站专题
当前位置:首页>网站开发>ASP教程>正文

实现搜索结果的关键词变色标注的程序


来源:我要学习网 时间:06-11-12 点击: 点击这里收藏本文

<% 
’ 以前写全文检索程序的时候写的. 
’ 原创 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 
   
%>


  把此文章收藏到:          
广而告之
文章搜索
  • Google JZxue.Com

关于我们 | 联系我们 | 友情链接 | 网站地图
Copyright © 2005 - 2006 建站学 All rights reserved.