I'm using the code on:
http://www.aspfree.com/c/a/ASP%20Code/Search-and-Replace-function-by-Meraj-Sami
I want to use two colors on two keywords, one in red, the second in blue.
This is the result:
http://n.1asphost.com/wheelofgod/highlighting.asp
(there is a third color with a third keyword but, never mind that)
The problem is that it's restarting the text once it has found and replaced the keyword in color.
schizo
10-04-2004, 04:48 PM
I really think they're overcomplicating the whole thing. See the code below.
<%
Function highlight(text, searchText, color)
text = Replace(text, searchText, "<span style=""color:" & color & """>" & searchText & "</span>")
highlight = text
End Function
Dim text_str
text_str = "Ezra 7:1 Now after these things, in the reign of Artaxerxes king of Persia, Ezra the son of Seraiah, the son of Azariah, the son of Hilkiah,"
text_str = highlight(text_str, "Ezra", "red")
text_str = highlight(text_str, "Artaxerxes", "blue")
Response.Write(text_str)
%>
gilgalbiblewhee
10-04-2004, 08:04 PM
The code works as it was written. But I needed to make some adjustments for my keyword searches of my text column of my database.
So I need the non-case sensitivity. What I don't understand is why does it restart writing the text when it replaces a keyword? see the url I had posted above.
javaNoobie
10-04-2004, 09:59 PM
<%
Function stringReplace(strSearchWithin,strSearchFor,color)
Dim lngStartingPosition
Dim lngFoundPosition
Dim strReplaced
'Set the start position
lngStartingPosition=1
lngFoundPosition=InStr(lngStartingPosition,strSearchWithin,strSearchFor,1)
do while lngFoundPosition > 0
'found
strReplaced=strReplaced & Mid(strSearchWithin,lngStartingPosition,lngFoundPosition-lngStartingPosition) & "<font color='" & color & "'>" & mid(strSearchWithin,lngFoundPosition,len(strSearchFor)) & "</font>"
lngStartingPosition=lngFoundPosition+len(strSearchFor)
lngFoundPosition=InStr(lngStartingPosition,strSearchWithin,strSearchFor,1)
Loop
stringReplace=strReplaced & Mid(strSearchWithin,lngStartingPosition) 'catch the last one
End Function
strSearch = "This is just a test string to test if the above function is working."
one= "is"
two="test"
three="working"
response.write(strSearch)
%>
will work on what you are trying to achieve when i got the time. For now, this does exactly what you wan but in longer way.
javaNoobie
10-04-2004, 10:28 PM
What I don't understand is why does it restart writing the text when it replaces a keyword?
Looking at your function here (http://www.webdeveloper.com/forum/showthread.php?s=&threadid=45700) you are actually concatenating strReplaced repeatedly, thus causing the repeat.
gilgalbiblewhee
10-04-2004, 11:35 PM
What do you mean by concatenate?
buntine
10-05-2004, 12:19 AM
It means to "join". VBScript uses the ampersand (&) character for concatenation.
Function stringReplace(strSearchWithin,strSearchFor,color)
^
So replaced <% with:
<SCRIPT LANGUAGE="VBSCRIPT" RUNAT="SERVER">
Response.Buffer = True
Response.Flush
and closed it.
So far so good.
But since I wanted to include the highlighting in my query "Keyword", "Keywordb", "Keywordc" in the Database field of text (called text_data) these are the changes that I made:
<SCRIPT LANGUAGE="VBSCRIPT" RUNAT="SERVER">
Response.Buffer = True
Response.Flush
Function stringReplace(strSearchWithin,strSearchFor,color)
Dim lngStartingPosition
Dim lngFoundPosition
Dim strReplaced
'Set the start position
lngStartingPosition=1
lngFoundPosition=InStr(lngStartingPosition,strSearchWithin,strSearchFor,1)
do while lngFoundPosition > 0
'found
strReplaced=strReplaced & Mid(strSearchWithin,lngStartingPosition,lngFoundPosition-lngStartingPosition) & "<font color='" & color & "'>" & mid(strSearchWithin,lngFoundPosition,len(strSearchFor)) & "</font>"
lngStartingPosition=lngFoundPosition+len(strSearchFor)
lngFoundPosition=InStr(lngStartingPosition,strSearchWithin,strSearchFor,1)
Loop
stringReplace=strReplaced & Mid(strSearchWithin,lngStartingPosition) 'catch the last one
End Function
</SCRIPT>
<%
strSearch = RS("text_data")
one=Keyword
two=Keywordb
three=Keywordc
For some reason the result page is taking too long to load the results and the script is timing out.
javaNoobie
10-18-2004, 10:33 PM
i assume you meant
one="Keyword"
two="Keywordb"
three="Keywordc"
instead of
one=Keyword
two=Keywordb
three=Keywordc
gilgalbiblewhee
10-18-2004, 11:53 PM
by putting "Keyword" I can get the word highlighted? Shouldn't it be & Keyword & or something. I'm asking because I wasn't able to test it. I hope it works though. Thanks.
javaNoobie
10-18-2004, 11:57 PM
I assumed that you were looking for the strings keyword, keywordb, keywordc. Hence I recommended that you encapsulate them in double quotes..
gilgalbiblewhee
10-19-2004, 09:37 AM
I didn't realize that "strSearchFor" had to be changed to "one", "two", "three". But this is the error.
Microsoft VBScript runtime error '800a01c2'
Wrong number of arguments or invalid property assignment: 'stringReplace'
/wheelofgod/highlight2.asp, line 28
Where am I going wrong?
<SCRIPT LANGUAGE="VBSCRIPT" RUNAT="SERVER">
Response.Buffer = True
Response.Flush
Function stringReplace(strSearchWithin,one,two,three,color)
Dim lngStartingPosition
Dim lngFoundPosition
Dim strReplaced
'Set the start position
lngStartingPosition=1
lngFoundPosition=InStr(lngStartingPosition,strSearchWithin,one,two,three,1)
do while lngFoundPosition > 0
'found
strReplaced=strReplaced & Mid(strSearchWithin,lngStartingPosition,lngFoundPosition-lngStartingPosition) & "<font color='" & color & "'>" & mid(strSearchWithin,lngFoundPosition,len(one)) & "</font>"
lngStartingPosition=lngFoundPosition+len(one)
lngFoundPosition=InStr(lngStartingPosition,strSearchWithin,one,1)
Loop
stringReplace=strReplaced & Mid(strSearchWithin,lngStartingPosition) 'catch the last one
End Function
</SCRIPT>
<%
Dim strSearchWithin,one,two,three
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
</SCRIPT>
<%
'OPTION EXPLICIT
Dim strText, strFind
strFind=Keyword
strText=RS("text_data")
strText= Highlight(strText, strFind,"<font color='red'><b>", "</b></font>")
'strFind=Keywordb
'strText=RS("text_data")
'strText= Highlight(strText, strFind,"<font color='blue'><b>", "</b></font>")
'strFind=Keywordc
'strText=RS("text_data")
'strText= Highlight(strText, strFind,"<font color='green'><b>", "</b></font>")
Response.Write strText
%>
But I put "'" at the end like 'strFind=Keywordb. What I need is to highlight Keyword in red as it does already, Keywordb in blue, and Keywordc in green.
javaNoobie
10-19-2004, 11:39 PM
Dim strText, strFind
strFind=Keyword
strText=RS("text_data")
strText= Highlight(strText, strFind,"<font color='red'><b>", "</b></font>")
'strFind=Keywordb
'strText=RS("text_data")
'strText= Highlight(strText, strFind,"<font color='blue'><b>", "</b></font>")
'strFind=Keywordc
'strText=RS("text_data")
'strText= Highlight(strText, strFind,"<font color='green'><b>", "</b></font>")
Response.Write strText
You are actually re-initialising strText to the recordset values. You only need to initialise it once.