|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Dreamweaver (all versions) section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
January 20th, 2006, 01:18 PM
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
highlight search words
hi guys,
i am using at the moment this type of regular expression to highlight a word from the search form.
function ShowBold(theField)
Set objRegExp= New RegExp
objRegExp.Pattern="(" & searchparam & ")"
objRegExp.IgnoreCase=True
objRegExp.Global=True
ShowBold=objRegExp.Replace(theField,"<b style=""background-color:#FFFF00; font-weight:bold;"">$1</b>")
end function
i would like to be able to highlight every ocurence of the world if there are more then one and the words are not beside each other.
Ex.
text: "i have a cd player that will play cd and also mp3"
if i search for cd player i get the "cd player" highlight it but not the cd on its own (the one in bold)
|
January 20th, 2006, 02:51 PM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi lucian,
Probably the easiest way to do this is to break apart your search term into an array based on a space. Then for each item in the array, call the highlighting code. That way, each individual word is highlighted.
HtH,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
January 20th, 2006, 04:03 PM
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
i am already doing this but i don't know where to use the expression:
Function BasicSearchSQL(Keyword)
Dim sKeyword
sKeyword = AdjustSql(Keyword)
BasicSearchSQL = ""
BasicSearchSQL = BasicSearchSQL & "[ProductName] LIKE '%" & sKeyword & "%' OR "
BasicSearchSQL = BasicSearchSQL & "[Description] LIKE '%" & sKeyword & "%' OR "
If Right(BasicSearchSQL, 4) = " OR " or Right(BasicSearchSQL, 4) = " or " Then BasicSearchSQL = Left(BasicSearchSQL, Len(BasicSearchSQL)-4)
End Function
' Function SetUpBasicSearch
' - Set up Basic Search parameter based on form elements pSearch & pSearchType
' - Variables setup: sSrchBasic
Sub SetUpBasicSearch()
Dim sSearch, sSearchType, arKeyword, sKeyword
sSearch = searchparam
sSearchType = searchtype
If sSearch <> "" Then
If sSearchType <> "" Then
While InStr(sSearch, " ") > 0
sSearch = Replace(sSearch, " ", " ")
Wend
arKeyword = Split(Trim(sSearch), " ")
For Each sKeyword In arKeyword
sSrchBasic = sSrchBasic & " " & BasicSearchSQL(sKeyword) & " " & sSearchType & " "
Next
Else
sSrchBasic = BasicSearchSQL(sSearch)
End If
End If
If Right(sSrchBasic, 4) = " OR " or Right(sSrchBasic, 4) = " or " Then sSrchBasic = Left(sSrchBasic, Len(sSrchBasic)-4)
If Right(sSrchBasic, 5) = " AND " or Right(sSrchBasic, 5) = " and " Then sSrchBasic = Left(sSrchBasic, Len(sSrchBasic)-5)
End Sub
then i use this to display the results coming from the database:
<%= ShowBold(x_ProductName) %>
|
January 20th, 2006, 04:45 PM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
You can pass the search term to ShowBold as well, and repeat the loop. The following (untested) code shows you what I mean:
Function ShowBold(theField, searchTerm)
Dim item
Set objRegExp = New RegExp
For Each item in Split(searchTerm, " ")
objRegExp.Pattern="(" & item & ")"
objRegExp.IgnoreCase = True
objRegExp.Global = True
ShowBold = objRegExp.Replace(theField,"<b style=""background-
color:#FFFF00; font-weight:bold;"">$1</b>")
Next
End Function
Then call it like this:
<%= ShowBold(x_ProductName, sSearch) %>
It may not work directly, but I am sure you get the idea...
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
January 20th, 2006, 07:00 PM
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
i have this:
function ShowBold(theField)
sSearchs = searchparam
While InStr(sSearchs, " ") > 0
sSearchs = Replace(sSearchs, " ", " ")
Wend
arKeywords = Split(Trim(sSearchs), " ")
Set objRegExp= New RegExp
For Each sKeywords In arKeywords
objRegExp.Pattern="(" & sKeywords & ")"
objRegExp.IgnoreCase=True
objRegExp.Global=True
ShowBold=objRegExp.Replace(theField,"<b style=""background-color:#FFFF00; font-weight:bold;"">$1</b>")
next
end function
but it only highlights the last word if i have more then one
|
January 20th, 2006, 07:27 PM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
My fault. This code keeps using the input text inside the loop, but it should reuse the result of each hightlighting:
Dim tempResult
tempResult = theField
sSearchs = searchparam
While InStr(sSearchs, " ") > 0
sSearchs = Replace(sSearchs, " ", " ")
Wend
arKeywords = Split(Trim(sSearchs), " ")
Set objRegExp= New RegExp
For Each sKeywords In arKeywords
objRegExp.Pattern="(" & sKeywords & ")"
objRegExp.IgnoreCase=True
objRegExp.Global=True
theField = objRegExp.Replace(theField,"<b style=""background-color:#FFFF00; font-weight:bold;"">$1</b>")
next
ShowBold = theField
Personally, I think it's cleaner to have ShowBold accept the search term as a parameter. That way, it's easier to reuse the method because it doesn't rely on global variables.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
January 21st, 2006, 04:30 AM
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thanks Imar. As usual you are right.
I was working on it all day yesterday, and the solution is so simple.
|
|
|