当前位置:首页 > 网页设计 > 写了几段与“关键词”相关的代码
Aug27th

写了几段与“关键词”相关的代码

diyidu 网页设计 0 人阅读了此文 我来说两句!

    闲着无聊的时候,我就喜欢摆弄这些莫名其妙的东西。今天心血来潮,一口气连写了3个有关“关键词”的代码函数,算是练习吧,不知道有没有人需要,需要的尽管拿去用吧,我会以此为荣的,呵呵。

    1.实现关键词红色标注的Asp函数

<%
dim patern
    dim found
   
    dim str
    dim result
   
    patern="(低一度)|(diyidu)"
    str="低一度{diyidu}出品,必是精品。请牢记我的永久域名:diyidu.cn。欢迎您收藏与订阅diyidu。"
    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
   
%>

    2.高亮显示关键词的代码


提示:您可以先修改部分代码再运行

    3.给内容中关键词加链接的代码


提示:您可以先修改部分代码再运行

本文出自:低一度,地址:http://www.diyidu.cn/post/keywords_code.html,转载须注明!

to "写了几段与“关键词”相关的代码" 引用该日志!

此楼已盖到第0层了,目前尚未封顶!