Link to home
Start Free TrialLog in
Avatar of GenesisTech
GenesisTech

asked on

Highlight Search Results in Classic ASP without searching HTML

I am using a routine to highlight ALL search results from a users search. it works REALLY GOOD except for a couple of specific situations. If part of the text being searched for matches the HTML being used to highlight the results, it bombs.

Example: If I search on "New Liber" the results out of the database look like this "New York tourist attraction Status of Liberty is a great thing to see"

The problem is that if the search is  "New Liber A" (for whatever reason) the A gets replaced INSIDE of the code that is currently being highlighted with additional HTML highlighting text. For example, the A in "spAn" gets replaced with <span class=""searchboldblue""> because it matches the search. So the HTML gets more HTML inserted inside of the existing HTML. Does that make sense?

This is my function:

'*****************************************************************************
' HIGHLIGHT function will search text for a specific string
' When string is found it will be surrounded by supplied strings
'
' NOTE: Unfortunately Replace() function does not preserve the original case
' of the found string. This function does.
'
' Parameters:
' strText        - string to search in
' strFind        - string to look for
' strBefore      - string to insert before the strFind
' strAfter       - string to insert after the strFind
'
' Example:
' This will make all the instances of the word "the" bold
'
' Response.Write Highlight(strSomeText, "the", "<span class=""searchboldblue"">", "</span>")
'
Function Highlight(strText, strFind)
         Dim nPos
         Dim nLen
         Dim nLenAll
       Dim strBefore, strAfter

         nLen = Len(strFind)
         nLenAll = nLen + Len(strBefore) + Len(strAfter) + 1
 
         Highlight = strText
 
         If nLen > 0 And Len(Highlight) > 0 Then
                 nPos = InStr(1, Highlight, strFind, 1)
                 Do While nPos > 0
                          Highlight = Left(Highlight, nPos - 1) & _
                                   strBefore & Mid(Highlight, nPos, nLen) & strAfter & _
                                   Mid(Highlight, nPos + nLen)
 
                          nPos = InStr(nPos + nLenAll, Highlight, strFind, 1)
                 Loop
         End If
End Function

Any ideas how to fix this?
Avatar of mankowitz
mankowitz
Flag of United States of America image

you have to parse the whole document for html strings in order to do this reliably.

Something like this:

Function parseMyHtml(ByVal htmlToParse) As String
    Dim htmlDocument As IHTMLDocument2 = New HTMLDocumentClass()
    htmlDocument.write(htmlToParse)
    htmlDocument.close()

    For Each element As IHTMLElement In htmlDocument.body.all
        element.innerText = Highlight(element.innerText, "the", "<span class=""searchboldblue"">", "</span>")
    Next

    Return htmlDocument.body.innerHTML
End Function
I had the same problem and i resolved it with regex

    Function HighlightText(strInput,arrtext )
        Set re = New RegExp 
        re.Pattern="(?!<.*?)(" & arrtext & ")(?![^<>]*?>)" 
        re.IgnoreCase = True 
        re.Global = True 
        strOutput = re.Replace(strInput,"<span class=""searchboldblue"">$&</span>") 
        HighlightText = strOutput
        set re = nothing
    end function

Open in new window

Avatar of GenesisTech
GenesisTech

ASKER

Manumd,

I am not real familiar with regex.

Can I use this with Classic ASP?

What am I passing to this function? What is it looking for? How do I pass multiple criteria (1 at a time or?)?

Thanks,

David
ASKER CERTIFIED SOLUTION
Avatar of GenesisTech
GenesisTech

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
None of the experts helped me and I figured it out myself.