Web Analytics Made Easy -
StatCounter highLight Search results - CodingForum

Announcement

Collapse
No announcement yet.

highLight Search results

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • highLight Search results

    i'm using the below code to highlight some keywords on a page, it works awesome, only 1 thing, it only highlights a part of a word..

    strSearch="cat"

    results = catacomb...

    is there a way to make the script highlight the whole word...

    example catacomb

    PHP Code:
    Function Highlight(strTextstrFindstrBeforestrAfter)
        
    Dim nPos
        Dim nLen
        Dim nLenAll
        
        nLen 
    Len(strFind)
        
    nLenAll nLen Len(strBefore) + Len(strAfter) + 1

        Highlight 
    strText

        
    If nLen And Len(Highlight) > 0 Then
            nPos 
    InStr(1HighlightstrFind1)
            Do While 
    nPos 0
                Highlight 
    Left(HighlightnPos 1) & _
                    strBefore 
    Mid(HighlightnPosnLen) & strAfter _
                    Mid
    (HighlightnPos nLen)

                
    nPos InStr(nPos nLenAllHighlightstrFind1)
            
    Loop
        End 
    If
    End Function 

  • #2
    Tested only briefly, and my regExp skills are... minimal... but try this:
    Code:
    function highlightWords(sSearchString, sCopyText)
    	dim oRegEx, oMatches, oMatch
    	set oRegEx = New RegExp
    	oRegEx.Global = True 
    	oRegEx.IgnoreCase = False
    	oRegEx.Pattern = "\b\S*("&sSearchString&")\S*\b"
    	set oMatches = oRegEx.Execute(sCopyText)
    	For Each oMatch in oMatches
    		sCopyText = replace(sCopyText, oMatch.value, "<span style=""color:yellow;"">" & oMatch.value & "</span>")
    	Next
    	highlightWords = sCopyText
    end function

    Comment

    Working...
    X