Forum Moderators: open

Message Too Old, No Replies

Replace Function

         

cmatcme

6:22 pm on Apr 15, 2005 (gmt 0)

10+ Year Member



Does anyone know of a functional replace function:

I wish to replace request("q") with " <strong> " & request("q") & " </strong> "

I wish to use on a search engine which I just built, this command will highlight the keyword the user selected. When I place this command at the top of my asp block, will it place strong round the title because if it does <strong> will appear in the title bar and if it does how do I resolve the problem.

cmatcme

Easy_Coder

2:24 am on Apr 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does this type of approach help you?

m_sSearchTokens = Request("q")

m_sTitle = m_sSearchTokens

<title><%= m_sTitle %></title>

Search results for <strong><%= sSearchTokens%></strong>

cmatcme

7:15 am on Apr 16, 2005 (gmt 0)

10+ Year Member



Creating a different variable solves the title problem but it doesn't bolden all of the keywords. For example:

User searches for example.

Search results for example:

Example webpage:
This is an example webpage created by example.com
http://example.com/examplewebpage.asp

Something like that and if the user were to type in webpage it would bolden all "webpage" on the page.

cmatcme

mattglet

12:15 pm on Apr 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Probably the most efficient way to conquer this is with a Regular Expression. I would build your search result, then fire through it with the RegExp to find any matches you're looking for.

cmatcme

2:58 pm on Apr 16, 2005 (gmt 0)

10+ Year Member



Found this piece of code:

 <%

DIM bolden
bolden = request("q")
response.write replace(bolden, "example","<strong> example </strong>")

%>

But there is one problem with it and that is that it doesn't apply to the whole page, only a variable and it will only replace a phrase inside the variable with something else. Is there a way to make it apply with the whole page? Additionally, the text is inside a variable, will it replace that too?

cmatcme

Easy_Coder

4:53 pm on Apr 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As you're uncorking your records you'll need to pass it all through a sorta MakeBold routine.

cmatcme

3:23 pm on Apr 18, 2005 (gmt 0)

10+ Year Member



As you're uncorking your records you'll need to pass it all through a sorta MakeBold routine.

But how?

cmatcme

Easy_Coder

4:39 pm on Apr 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you using a recordset or an array?

cmatcme

5:07 pm on Apr 18, 2005 (gmt 0)

10+ Year Member



Arrays yes.

cmatcme

Easy_Coder

10:43 pm on Apr 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try something like this...


searchToken = Request("q")
public function MakeBold(in_dbString, in_originalRequest)
Dim retVal
if in_dbString = in_originalRequest then
retVal = "<strong>" & in_dbString & "</strong>"
else
retVal = in_dbString
end if
MakeBold = retVal
end function
for x = lbound(aResults,2) to ubound(aResults,2)
Response.Write MakeBold(aResults(0,x), searchToken)
next

cmatcme

3:27 pm on Apr 19, 2005 (gmt 0)

10+ Year Member



Thanks for the code.

The code:

<%

searchToken = Request("q")
public function MakeBold(in_dbString, in_originalRequest)
Dim retVal
if in_dbString = in_originalRequest then
retVal = "<strong>" & in_dbString & "</strong>"
else
retVal = in_dbString
end if
MakeBold = retVal
end function
for x = lbound(aResults,2) to ubound(aResults,2)
Response.Write MakeBold(aResults(0,x), searchToken)
next

%>

Hello I think <% = request("q") %> is really cool. Everyone likes <% = request("q") %>.
<% = request("q") %> rules! [/code]

returns...

Type mismatch lbound

Any ideas?

cmatcme

That code is much appreaciated!

Easy_Coder

5:21 pm on Apr 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



cmatcme -

You need to populate your array that you mentioned you had. Then make sure the name of your array lines up with the name I put into that code.

This sample populates the array with sample junk:


searchToken = request("q")
public function MakeBold(in_dbString, in_originalRequest)
Dim retVal
if lcase(in_dbString) = lcase(in_originalRequest) then
retVal = "<strong>" & in_dbString & "</strong>"
else
retVal = in_dbString
end if
MakeBold = retVal
end function
Dim aResults
ReDim aResults(0,4)
aResults(0,0) = "Test"
aResults(0,1) = "to"
aResults(0,2) = "see"
aResults(0,3) = "me"
aResults(0,4) = "work"
for x = lbound(aResults,2) to ubound(aResults,2)
Response.Write MakeBold(aResults(0,x), searchToken) & "<br>"
next