Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

making the word search bold 1

Status
Not open for further replies.

orangeblue

Programmer
Nov 26, 2002
107
GB
Hi

I have crated a search script but i want to make the words that equal the searched words bold

Say if i have a block of text

Str="hello world this is england calling and so on "

the word searched is "england" how would i go about making it bold.

the best example is the google results section

how would i have to search the string character by character

cheers
hesh
 
take a look at this thread...a very good resource

thread333-863709

-DNG
 
Not sure if those methods are good, i am sure they are, ths is one i came up with that seems to work, and doesnt change the case of the words it finds.
this has 2 modes. And and Or. Send a url variable andor=or and it will highlight every word in the search string where ever is appears in the string, send andor=and and it will only highlight the exact phrase. Hope it helps. Also this one will highlight plurals, so if the search term is sport it will also highlight sports

Code:
function highlight(text)
	temp = text
	search=simplify(request("search"))
	if search <> "" then
	dim RegularExpressionObject
	Set RegularExpressionObject = New RegExp
	if request("andor") = "or" then
		searchStr = split(search," ")
		for each item in searchStr
			With RegularExpressionObject
			.Pattern = "\b"&item&"s?\b"
			.IgnoreCase = True
			.Global = True
			End With
			set Matches = RegularExpressionObject.execute(temp)
			For Each Match in Matches   ' Iterate Matches collection.
				temp = RegularExpressionObject.replace(temp,"<span class='highlight'>"&tCase(Match.Value)&"</span>")
			Next
		next
	else
		With RegularExpressionObject
		.Pattern = "\b"&search&"s?\b"
		.IgnoreCase = True
		.Global = True
		End With
		set Matches = RegularExpressionObject.execute(temp)
		For Each Match in Matches   ' Iterate Matches collection.
			temp = RegularExpressionObject.replace(temp,"<span class='highlight'>"&tCase(Match.Value)&"</span>")
  		Next
	end if
	end if
	highlight = temp
end function
%>
<%function simplify(str)
	dim RegularExpressionObject
	Set RegularExpressionObject = New RegExp
	With RegularExpressionObject
	.Pattern = "\[|\]|\\|\/|\?|@|'|\#|\band\b|\bor\b|\bis\b|\bif\b"
	.IgnoreCase = True
	.Global = True
	End With
	str = RegularExpressionObject.replace(str," ")
	str=ltrim(str)
	str=rtrim(str)
	simplify=str

end function%>

}...the bane of my life!
 
ah also there is a function called simplify, delete that as you don't need it lol

here is the modified code, send it 3 parameters, the search(search) and or (andor) and the str (text)
Code:
function highlight(search,andor,text)
    temp = text
    if search <> "" then
    dim RegularExpressionObject
    Set RegularExpressionObject = New RegExp
    if request("andor") = "or" then
        searchStr = split(search," ")
        for each item in searchStr
            With RegularExpressionObject
            .Pattern = "\b"&item&"s?\b"
            .IgnoreCase = True
            .Global = True
            End With
            set Matches = RegularExpressionObject.execute(temp)
            For Each Match in Matches   ' Iterate Matches collection.
                temp = RegularExpressionObject.replace(temp,"<span class='highlight'>"&(Match.Value)&"</span>")
            Next
        next
    else
        With RegularExpressionObject
        .Pattern = "\b"&search&"s?\b"
        .IgnoreCase = True
        .Global = True
        End With
        set Matches = RegularExpressionObject.execute(temp)
        For Each Match in Matches   ' Iterate Matches collection.
            temp = RegularExpressionObject.replace(temp,"<span class='highlight'>"&(Match.Value)&"</span>")
          Next
    end if
    end if
    highlight = temp
end function
%>

}...the bane of my life!
 
ok another alteration (this one makes it work!!)

Code:
<%
function highlight(search,andor,text)
	temp = text
	if search <> "" then
	dim RegularExpressionObject
	Set RegularExpressionObject = New RegExp
	if request("andor") = "or" then
		searchStr = split(search," ")
		for each item in searchStr
			With RegularExpressionObject
			.Pattern = "\b"&item&"s?\b"
			.IgnoreCase = True
			.Global = True
			End With
			set Matches = RegularExpressionObject.execute(temp)
			RegularExpressionObject.IgnoreCase = False
			For Each Match in Matches   ' Iterate Matches collection.
				RegularExpressionObject.Pattern = "\b"&Match.Value&"\b"
				temp = RegularExpressionObject.replace(temp,"<#span class='highlight'>"&(Match.Value)&"<#/span>")
				i=i+1
			Next
		next
	else
		With RegularExpressionObject
		.Pattern = "\b"&search&"s?\b"
		.IgnoreCase = True
		.Global = True
		End With
		set Matches = RegularExpressionObject.execute(temp)
		RegularExpressionObject.IgnoreCase = False
		For Each Match in Matches   ' Iterate Matches collection.
			RegularExpressionObject.Pattern = "\b"&Match.Value&"\b"
			temp = RegularExpressionObject.replace(temp,"<#span class='highlight'>"&(Match.Value)&"<#/span>")
			i=i+1
		Next
	end if
	end if

	temp = replace(temp,"<#span class='highlight'><#span class='highlight'>","<span class='highlight'>")
	temp = replace(temp,"<#span class='highlight'>","<span class='highlight'>")
	temp = replace(temp,"<#/span><#/span>","<#/span>")
	temp = replace(temp,"<#/span>","</span>")
	
	highlight = temp
end function
%>

}...the bane of my life!
 
Why not doing that client-side? With recursive DOM tree traversal function (javascript) and almost no regexp...

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
Set re.Global to False before you start the for loop to replace the matches! Otherwise you'll replace all occurrences with whatever the first match is! Or use normal Replace instead of re.replace. You already know the exact string matched from the Match.Value attribute, so you don't need a regular expression to match it.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top