1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

highlighting an array of keywords

Discussion in 'Programming' started by gilgalbiblewheel, Jul 20, 2005.

  1. #1
    
    <%
    strFind = [COLOR=Red]Keyword[/COLOR] '"jeSus christ joseph begat"
    strText = [COLOR=Red]rs("text_data")[/COLOR]'"And Jacob begat Joseph the husband of Mary, of whom was born Jesus, who is called Christ."
    [COLOR=RoyalBlue]response.write Highlight(strText, strFind)[/COLOR]
    %>
    <SCRIPT LANGUAGE="VBSCRIPT" RUNAT="SERVER">
    Function Highlight(strText, strFind)
     '// Check that at least one search term has been submitted
     If Len(strFind) < 1 Then
      '// no search term entered. Exit the function
      Highlight = "No search term entered"
      Exit Function
     End If
     
     '// define the colours to be used to highlight search results
     Dim strColors : strColors = "#FF0000" ',#008000,#0000FF
     '// and split them into an array
     Dim arrColors : arrColors = Split(strColors,",")
     
     '// split the search terms into an array
     Dim arrFind : arrFind = Split(strFind," ")
     
     '// Initialize the regular expression object to perfom the search
     Dim oRegExp, oMatches, sMatch
     Set oRegExp = New RegExp
     oRegExp.Global = True '// returns all matches to the search term
     oRegExp.IgnoreCase = True '// Case insensitive
     
     '// loop through the array of search terms to find matches
     Dim i, strHighlight
     For i = 0 to UBound(arrFind)
      oRegExp.Pattern = arrFind(i) '// sets the search pattern string
      Set oMatches = oRegExp.Execute(strText) '// performs the search 
      for each match in oMatches
      '// build the code to be used to highlight results
      strHighlight = "<font color='[COLOR=Red]red[/COLOR]'><b>" & match.value & "</b></font>" '" & arrColors(i) & "
      next
      '// then replace matches from the search with the above code
      strText = oRegExp.Replace(strText, strHighlight)
     Next
     '// release the RegExp object
     Set oRegExp = Nothing
     '// and return the result
     Highlight = strText
    End Function
    </SCRIPT>
    
    Code (markup):
    SO I made the changes but whenever I insert the Keyword and rs("text_data")

    I get this error:
     
    gilgalbiblewheel, Jul 20, 2005 IP