| Author |
Topic Search Topic Options
|
amisima
Newbie
Joined: 09 June 2003
Status: Offline
Points: 17
|
Post Options
Thanks(0)
Quote Reply
Topic: highlight results problem with replace Posted: 23 July 2003 at 8:05am |
|
Hi I am trying to highlight my search results. I have a replace function, when I highlight the search result the rest of my results changes into upper case. This is what my code looks like:
<%if InStr(ucase(rs("EN")),ucase(search_criteria)) <> 0 then %>
<td><B>{EN}</B></td>
<%
pos=InStr(ucase(rs("EN")),ucase(search_criteria))
chunk=Mid(rs("EN"),pos,Len(search_criteria))
MyHighlight=Replace(ucase(rs("EN")), ucase(search_criteria), "<SPAN STYLE=background:#FFFF00>"& chunk &"</SPAN>")
%>
<td><b><%Response.Write (MyHighlight)%></b></td><%
else
if rs("EN") <> empty then
%>
<td><B>{EN}</B></td>
<td><b><%response.write(rs("EN"))%></b></td><%
end if
end if%>
can anyone help?
Thanks
Amisima
|
 |
Flamewave
Senior Member
Joined: 19 June 2002
Location: United States
Status: Offline
Points: 376
|
Post Options
Thanks(0)
Quote Reply
Posted: 23 July 2003 at 10:40am |
Try this:
<% If InStr(Ucase(rs("EN")), Ucase(search_criteria)) <> 0 Then %> <td><b>{EN}</b></td> <% pos = InStr(Ucase(rs("EN")), Ucase(search_criteria)) chunk = Mid(rs("EN"), pos, Len(search_criteria)) MyHighlight = Replace(rs("EN"), search_criteria, "<SPAN STYLE='background:#FFFF00;'>"& chunk &"</SPAN>", 1, -1, 1) %> <td><b><%= MyHighlight %></b></td> <% Else If rs("EN") <> Empty Then %> <td><b>{EN}</b></td> <td><b><%= rs("EN") %></b></td> <% End If End If %>
|
|
- Flamewave
They say the grass is greener on the other side, but if you really think about it, the grass is greener on both sides.
|
 |
amisima
Newbie
Joined: 09 June 2003
Status: Offline
Points: 17
|
Post Options
Thanks(0)
Quote Reply
Posted: 23 July 2003 at 11:07am |
|
I will try that thanks
|
 |
KCWebMonkey
Senior Member
Go Chiefs!
Joined: 21 June 2002
Status: Offline
Points: 1319
|
Post Options
Thanks(0)
Quote Reply
Posted: 23 July 2003 at 11:15am |
|
I think this would be a good tutorial in the ASP Tutorials section of WWG. This gets asked about once per month...
|
 |
michael
Senior Member
Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
|
Post Options
Thanks(0)
Quote Reply
Posted: 23 July 2003 at 12:11pm |
There is a better way to do that IMHO. I faced the same problem that it was disturbing the case of your result so I am using the following function:
Function Highlight(strText, strFind, strBefore, strAfter) Dim nPos Dim nLen Dim nLenAll 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
Usage example: itext = Highlight(itext, yoursearchword,"<span style='background:yellow'>", "</span>")
|
|
|
 |
Flamewave
Senior Member
Joined: 19 June 2002
Location: United States
Status: Offline
Points: 376
|
Post Options
Thanks(0)
Quote Reply
Posted: 23 July 2003 at 9:16pm |
Just an FYI, when you are using the Replace function in ASP, if you are having probelms with case sensitivity, set the last parameter of the function to 1, that way it will do a textual compare instead of a binary compare, and it doesnt care about case then. For more information on the Replace function, visit: http://www.w3schools.com/vbscript/func_replace.asp
|
|
- Flamewave
They say the grass is greener on the other side, but if you really think about it, the grass is greener on both sides.
|
 |
michael
Senior Member
Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
|
Post Options
Thanks(0)
Quote Reply
Posted: 24 July 2003 at 7:30am |
|
Tried that Flame, it does not work.
|
|
|
 |
Flamewave
Senior Member
Joined: 19 June 2002
Location: United States
Status: Offline
Points: 376
|
Post Options
Thanks(0)
Quote Reply
Posted: 24 July 2003 at 4:18pm |
|
Weird, I've never had a problem with it...
|
|
- Flamewave
They say the grass is greener on the other side, but if you really think about it, the grass is greener on both sides.
|
 |