Print Page | Close Window

ASP Replace function in search results

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=13847
Printed Date: 30 March 2026 at 2:51pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: ASP Replace function in search results
Posted By: webbasterd
Subject: ASP Replace function in search results
Date Posted: 16 February 2005 at 3:50pm
Hi all,
 
I've just made my first search engine that search trough an MySQL database on a corporate portal. Everyting works fine except for one thing. When I retrieve a database record based on a search word, I use the REPLACE function to make the search word in the recordset

Highlighted. I used the source from WWF as an example.

The problem is that when I use upercase words as a search words, the lowercase words in the dataset are replaced by upercase words. e.g. "HeLo WoRlD" as the search criteria will replace "Hello world" in my search results by "HeLo WoRlD".

The same problem occurs in the search function of WWF 7.9. (I've noticed that this problem is solved in WWF 8.0 A) Does anyone knows how I can solve this for my Search engine? (i've search the forum but didn't find a workarround)
 
Thanks in advance.
 
--
Alexander



Replies:
Posted By: michael
Date Posted: 16 February 2005 at 9:43pm
Below is the code i use for an application, it may be possible to do simpler but works well for me. I use the code in .net so you cannot use the searchoption enum like that, you can actually take it out if you want, but basically I want to highlight all words if I do an OR search, but only a specific sentence for for a phrase search (even if some of the words exist somewhere else)

Public Enum SearchOption

_OR

_PHRASE

End Enum

Private Function Highlight(ByVal strText As String, ByVal strFind As String) As String

Dim nPos

Dim nLen

Dim nLenAll

Dim strBefore, strAfter As String

strBefore = "<span style='background:yellow'>"

strAfter = "</span>"

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

Public Function DOHighLight(ByVal strText As String, ByVal strFind As String, ByVal Opt As SearchOption) As String

Select Case Opt

Case SearchOption._OR

Dim sterm As Object = Split(strFind, " ")

Dim mterm As Object = UBound(sterm)

Dim counter As Integer

For counter = 0 To mterm

strText = Highlight(strText, sterm(counter))

Next

Case SearchOption._PHRASE

strText = Highlight(strText, strFind)

End Select

Return strText

End Function



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


Posted By: michael
Date Posted: 16 February 2005 at 9:47pm
forgot to mention, strText is the whole text and strFind is the keyword(s) you want to replace.

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


Posted By: webbasterd
Date Posted: 20 February 2005 at 5:26pm
Thanks... I will try this out



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