Print Page | Close Window

highlight results problem with replace

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=4461
Printed Date: 30 March 2026 at 10:06am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: highlight results problem with replace
Posted By: amisima
Subject: highlight results problem with replace
Date 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



Replies:
Posted By: Flamewave
Date 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.


Posted By: amisima
Date Posted: 23 July 2003 at 11:07am
I will try that thanks


Posted By: KCWebMonkey
Date 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...


Posted By: michael
Date 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>")



-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: Flamewave
Date 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 - 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.


Posted By: michael
Date Posted: 24 July 2003 at 7:30am
Tried that Flame, it does not work.

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: Flamewave
Date 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.


Posted By: michael
Date Posted: 25 July 2003 at 8:25am
Let me rephrase that. If you have a function that replaces the searched word, with your searchword plus the highligh it takes your original and replaces it with the word the way it has been entered so the case flag is just so it ignores cases in search comparison.

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net