HighLight Function:
HighLight keywords in a string.
Description:
The highlight function searches through the string passed to it for either a string or array of keywords. It highlights the keywords found in the string by wrapping them in HTML SPAN tags. If an array of keywords is passed to it, it highlights the matches in the order of longest keyword to shortest. Setting the boolPhrase argument either false or true an highlight of any word or exact phrase is performed respectively.
Syntax:
strHighLighted = HighLight(stringToHighlight, strKeywords, boolPhrase)
Details:
Arguments: stringToHighlight = string to highlight strKeyWords = word or array of words to highlight (separated by space or comma) boolPhrase = true = exact phrase false = any word
Change the variable: HlColor = "yellow" with the highlight background color you prefer
Example:
<%
Dim a, strText
strText = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Praesent vehicula. Nam rem risus, scelerisque condimentum, ultricies sed, pharetra commodo, nibh. Suspendisse eu purus sit amet velit facilisis gravida. Nulla magna nulla, convallis vel, congue id, tempor et, ipsum. Nunc rhoncus."
a = highlight(strText, "gravida adipiscing lorem", false)
Response.Write a '--- returns 4 highlighted word matches
%>
ASP Source Code:
<%
Private Function HighLight(ByVal strToHighlight, ByVal strKeywords, ByVal bPhrase) '--- strToHighlight = string to highlight '--- strKeyWords = word or array of words to search for '--- bPhrase = highlight: true=exact phrase, false=any word Dim i, j, iKwd, strkwd Dim matchPos1 Dim matchPos2 Dim matchPos3 Dim badChar Dim strBegin Dim strEnd Dim tempStoreKeyword Dim objKWRegExp Dim objLRegExp Dim objStripHTMLRegExp Dim HlColor HlColor = "yellow" '--- highlighting bgcolor Set objStripHTMLRegExp = New RegExp objStripHTMLRegExp.IgnoreCase = True objStripHTMLRegExp.Global = True objStripHTMLRegExp.Pattern = "<\S+>" strToHighlight = objStripHTMLRegExp.Replace(strToHighlight, " ") Set objLRegExp = New RegExp objLRegExp.Global = True objLRegExp.Pattern = "(\+|\?|\$|\(|\)|\.|\||\{|\}|\[|\]|\^|\*|\\)" Set objKWRegExp = New RegExp objKWRegExp.IgnoreCase = True objKWRegExp.Global = True strKeywords = Trim(strKeywords) strKeywords = Replace(strKeywords, ",", " ") strKeywords = Replace(strKeywords, ";", " ") If Not bPhrase Then strKeywords = Split(strKeywords, " ") End if If Not IsArray(strKeywords) Then strKwd = Replace(strKeywords, """, """") objKWRegExp.Pattern = "()(" & objLRegExp.Replace(strKwd, "\$1") & ")()" strToHighlight = objKWRegExp.Replace(strToHighlight, "$1<span style='background-color: " & _ HlColor & ";'>$2</span>$3") Else For i = 0 To UBound(strKeywords) - 1 For j = i + 1 To UBound(strKeywords) if Len(strKeywords(i)) < Len(strKeywords(j)) Then tempStoreKeyword = strKeywords(i) strKeywords(i) = strKeywords(j) strKeywords(j) = tempStoreKeyword End if Next Next Do While (iKwd <= UBound(strKeywords)) And (InStr(strToHighlight, "<span") = 0) objKWRegExp.Pattern = "()(" & objLRegExp.Replace(strKeywords(iKwd), "\$1") & ")()" strToHighlight = objKWRegExp.Replace(strToHighlight, "$1<span style='background-color: " & _ HlColor & ";'>$2</span>$3") iKwd = iKwd + 1 Loop For i = iKwd to UBound(strKeywords) matchPos1 = InStr("<span style='background-color: " & _ HlColor & ";'></span>", LCase(strKeywords(i))) matchPos2 = matchPos1 + Len(strKeywords(i)) matchPos1 = matchPos1 - 1 matchPos3 = InstrRev("<span style='background-color: " & _ HlColor & ";'></span>", LCase(strKeywords(i))) - 1 if matchPos1 > -1 then if matchPos3 = matchPos1 then badChar = Mid("<span style='background-color: " & HlColor & ";'></span>", matchPos1, 1) if badChar <> " " then strBegin = "([^" & badChar & "])(" Else strBegin = "()(" badChar = Mid("<span style='background-color: " & HlColor & ";'></span>", matchPos2, 1) if badChar <> " " then strEnd = ")([^" & badChar & "])" Else strEnd = ")()" objKWRegExp.Pattern = strBegin & objLRegExp.Replace(strKeywords(i), "\$1") & strEnd strToHighlight = objKWRegExp.Replace(strToHighlight, "$1<span style='background-color: " & _ HlColor & ";'>$2</span>$3") end if else objKWRegExp.Pattern = "()(" & objLRegExp.Replace(strKeywords(i), "\$1") & ")()" strToHighlight = objKWRegExp.Replace(strToHighlight, "$1<span style='background-color: " & _ HlColor & ";'>$2</span>$3") end if Next End If HighLight = strToHighlight End Function '--- HighLight
%>
|