Click to See Complete Forum and Search --> : how to highlight in three colours? have code but


gilgalbiblewhee
10-02-2004, 01:11 AM
There is something wrong in this. I couldn't figure it out.

I have three: Keyword, Keywordb, Keywordc
I had inserted: strSearchFor, two, three (same function)


<SCRIPT LANGUAGE="VBSCRIPT" RUNAT="SERVER">
Function stringReplace(strSearchWithin,strSearchFor,two,three)
Dim lngStartingPosition
Dim lngFoundPosition
Dim strReplaced
'Set the start position
lngStartingPosition=1
lngFoundPosition=InStr(lngStartingPosition,strSearchWithin,strSearchFor,1)
lngFoundPosition=InStr(lngStartingPosition,strSearchWithin,two,1)
lngFoundPosition=InStr(lngStartingPosition,strSearchWithin,three,1)
do while lngFoundPosition > 0
'found
strReplaced=strReplaced & Mid(strSearchWithin,lngStartingPosition,lngFoundPosition-lngStartingPosition) & "<font color='red'>" & "<b>" & mid(strSearchWithin,lngFoundPosition,len(strSearchFor)) & "</b>" & "</font>"

strReplaced=strReplaced & Mid(strSearchWithin,lngStartingPosition,lngFoundPosition-lngStartingPosition) & "<font color='blue'>" & "<b>" & mid(strSearchWithin,lngFoundPosition,len(two)) & "</b>" & "</font>"

strReplaced=strReplaced & Mid(strSearchWithin,lngStartingPosition,lngFoundPosition-lngStartingPosition) & "<font color='green'>" & "<b>" & mid(strSearchWithin,lngFoundPosition,len(three)) & "</b>" & "</font>"


lngStartingPosition=lngFoundPosition+len(strSearchFor)
lngStartingPosition=lngFoundPosition+len(two)
lngStartingPosition=lngFoundPosition+len(three)


lngFoundPosition=InStr(lngStartingPosition,strSearchWithin,strSearchFor,1)
lngFoundPosition=InStr(lngStartingPosition,strSearchWithin,two,1)
lngFoundPosition=InStr(lngStartingPosition,strSearchWithin,three,1)
Loop
stringReplace=strReplaced & Mid(strSearchWithin,lngStartingPosition) 'catch the last one
End Function
</SCRIPT>

<%

strSearchWithin=RS("res_text_data")
strSearchFor=Keyword
two=Keywordb
three=Keywordc
Response.Write stringReplace(strSearchWithin,strSearchFor,two,three)
%>

javaNoobie
10-02-2004, 11:31 AM
I am guessing that

strSearchFor="Keyword"
two="Keywordb"
three="Keywordc"

and that you should use 3 different "lngFoundPosition" since you are looking for 3 different strings.

gilgalbiblewhee
10-02-2004, 01:40 PM
Here are what I tripled:
Dim lngStartingPosition
Dim lngStartingTwoPosition
Dim lngStartingThreePosition
Dim lngFoundPosition
Dim lngFoundTwoPosition
Dim lngFoundThreePosition
Dim strReplaced
Dim strTwoReplaced
Dim strThreeReplaced


In this:

<SCRIPT LANGUAGE="VBSCRIPT" RUNAT="SERVER">
Function stringReplace(strSearchWithin,strSearchFor,two,three)
Dim lngStartingPosition
Dim lngStartingTwoPosition
Dim lngStartingThreePosition
Dim lngFoundPosition
Dim lngFoundTwoPosition
Dim lngFoundThreePosition
Dim strReplaced
Dim strTwoReplaced
Dim strThreeReplaced

'Set the start position
lngStartingPosition=1
lngStartingTwoPosition=1
lngStartingThreePosition=1
lngFoundPosition=InStr(lngStartingPosition,strSearchWithin,strSearchFor,1)
lngFoundTwoPosition=InStr(lngStartingTwoPosition,strSearchWithin,two,1)
lngFoundThreePosition=InStr(lngStartingThreePosition,strSearchWithin,three,1)
do while lngFoundPosition > 0 AND lngFoundTwoPosition > 0 AND lngFoundThreePosition > 0
'found

strReplaced=strReplaced & Mid(strSearchWithin,lngStartingPosition,lngFoundPosition-lngStartingPosition) & "<font color='red'>" & "<b>" & mid(strSearchWithin,lngFoundPosition,len(strSearchFor)) & "</b>" & "</font>"
lngStartingPosition=lngFoundPosition+len(strSearchFor)
lngFoundPosition=InStr(lngStartingPosition,strSearchWithin,strSearchFor,1)

strTwoReplaced=strTwoReplaced & Mid(strSearchWithin,lngStartingTwoPosition,lngFoundTwoPosition-lngStartingTwoPosition) & "<font color='blue'>" & "<b>" & mid(strSearchWithin,lngFoundTwoPosition,len(two)) & "</b>" & "</font>"
lngStartingTwoPosition=lngFoundTwoPosition+len(two)
lngFoundTwoPosition=InStr(lngStartingTwoPosition,strSearchWithin,two,1)

strThreeReplaced=strThreeReplaced & Mid(strSearchWithin,lngStartingThreePosition,lngFoundThreePosition-lngStartingThreePosition) & "<font color='green'>" & "<b>" & mid(strSearchWithin,lngFoundThreePosition,len(three)) & "</b>" & "</font>"
lngStartingThreePosition=lngFoundThreePosition+len(three)
lngFoundThreePosition=InStr(lngStartingThreePosition,strSearchWithin,three,1)

Loop
stringReplace=strReplaced & Mid(strSearchWithin,lngStartingPosition) 'catch the last one
stringTwoReplace=strTwoReplaced & Mid(strSearchWithin,lngStartingTwoPosition) 'catch the last one
stringThreeReplace=strThreeReplaced & Mid(strSearchWithin,lngStartingThreePosition) 'catch the last one
End Function


</SCRIPT>
<%
strSearchWithin=RS("res_text_data")
strSearchFor=Keyword
two=Keywordb
three=Keywordc
Response.Write stringReplace(strSearchWithin,strSearchFor,two,three)
%>

javaNoobie
10-04-2004, 02:54 AM
Just wondering why dont u use Replace()?

strSearch = "This is just a test string to test if the replace function is working."
strSearchFor= "is"
two="test"
three="working"

strSearch = Replace(strSearch, strSearchFor, "<font color=""#FF0000"">" & strSearchFor & "</font>")
strSearch = Replace(strSearch, two, "<font color=""#00FF00"">" & two & "</font>")
strSearch = Replace(strSearch, three, "<font color=""#0000FF"">" & three & "</font>")
Response.write(strSearch)

gilgalbiblewhee
10-04-2004, 02:12 PM
Two websites said that there is problem in case-ing.

<SCRIPT LANGUAGE="VBSCRIPT" RUNAT="SERVER">
Function stringReplace(strSearchWithin,strSearchFor,two,three)
Dim lngStartingPosition
Dim lngFoundPosition
Dim lngFoundtwoPosition
Dim lngFoundthreePosition
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='red'>" & 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
'Set the start position
lngStartingPosition=1
lngFoundtwoPosition=InStr(lngStartingPosition,strSearchWithin,two,1)
do while lngFoundtwoPosition > 0
'found
strReplaced=strReplaced & Mid(strSearchWithin,lngStartingPosition,lngFoundtwoPosition-lngStartingPosition) & "<font color='blue'>" & mid(strSearchWithin,lngFoundtwoPosition,len(two)) & "</font>"
lngStartingPosition=lngFoundtwoPosition+len(two)
lngFoundtwoPosition=InStr(lngStartingPosition,strSearchWithin,two,1)
Loop
stringReplace=strReplaced & Mid(strSearchWithin,lngStartingPosition) 'catch the last one

End Function
</SCRIPT>
<%
OPTION EXPLICIT
Dim strSearchWithin,strSearchFor,two,three
strSearchWithin="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, "
strSearchFor="Ezra"
two="Artaxerxes"
three="king"
Response.Write stringReplace(strSearchWithin,strSearchFor,two,three)
%><br><br>
<font color='red'>ezra</font> <font color='blue'>artaxerxes</font>
<br><br>
Original phrase:<br>
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,
<br><br>

The problem is that it's restarting the text once it has found and replaced the keyword in color.

javaNoobie
10-06-2004, 12:13 AM
you will have to re-assign searchWithin to the new searchWithin that contains the highlighted text.

stringReplace=strReplaced & Mid(strSearchWithin,lngStartingPosition) 'catch the last one
strsearchWithin = stringReplace
'Set the start position
lngStartingPosition=1
lngFoundtwoPosition=InStr(lngStartingPosition,strSearchWithin,two,1)