Highlighting search results
I don't know if this would be an ASP thing, a javascript thing, a CGI thing, a Visual Basic thing, or just plain impossible ...
On some sites, when you do a search the results pages have the search terms highlighted in some way: different color or font, bolding, etc. So if you searched on "computer", every instance of the string "computer" in the results stands out.
How is this done? My pages are written in ASP, with the search results being generated dynamically from the database and put into a table. I would imagine one needs to do some kind of "post processing" on the output for applying the desired effect to the search strings that were found?
Thanks,
Kristen
Kristen
http://www.RewardsLookup.com and
http://www.cancerdrugfeedback.net still under early development
Personal page:http://www.isd.net/kwong/kristen.html
Maverick posted this at 02:53 — 4th August 2000.
They have: 334 posts
Joined: Dec 1999
Well, if you'd like to abandon ASP and move to a Unix server, I could tell you how to do it in PHP
I'm not sure what the coding is for ASP, but the principle should be the same. You've already got the search string stored as some variable since that's what you're using to query the database. After you've done the query and gotten the result, do a string replace on the result and replace the search string with the string plus tags to highlight it.
So if your the word searched for was in a variable named $searchstring and the query results were in $result
In PHP it would be something like:
$result = str_replace ("$searchstring", "<B>$searchstring</B>", $result);
Then you plug that into the table that displays the results. If they searched for the word computer and the db query returned "this is a page about computers" the result would look like:
This is a page about computers
I'm sure that would work in PHP, in ASP try this:
// instr: string to search in.
// oldstr: string to search for, ignoring the case.
// newstr: string replacing the occurences of oldstr.
CString ReplaceNoCase( LPCTSTR instr, LPCTSTR oldstr, LPCTSTR newstr )
{
CString output( instr );
// lowercase-versions to search in.
CString input_lower( instr );
CString oldone_lower( oldstr );
input_lower.MakeLower();
oldone_lower.MakeLower();
// search in the lowercase versions,
// replace in the original-case version.
int pos=0;
while ( (pos=input_lower.Find(oldone_lower,pos))!=-1 ) {
// need for empty "newstr" cases.
input_lower.Delete( pos, lstrlen(oldstr) );
input_lower.Insert( pos, newstr );
// actually replace.
output.Delete( pos, lstrlen(oldstr) );
output.Insert( pos, newstr );
}
return output;
}
No guarantees that it'll work though
[Edited by Maverick on 08-03-2000 at 11:00 PM]
KLWong posted this at 14:23 — 4th August 2000.
They have: 135 posts
Joined: Apr 2000
Thanks for your very helpful reply. I don't have my ASP books at work, but I think there's a VB search & replace type of function that I can use. If not, I'll follow the example of your code to write my own. Why I didn't think of this relatively obvious scheme before, I have no idea...
(As long as I want to have the option of working on my site from work -- like during lunch, after hours, etc. -- I'm stuck with an NT/ASP/Access database implementation, because I'm not allowed to load any "non-authorized" applications onto my work PC.)
Thanks again,
Kristen
Kristen
http://www.RewardsLookup.com and
http://www.cancerdrugfeedback.net still under early development
Personal page:http://www.isd.net/kwong/kristen.html
KLWong posted this at 21:22 — 7th August 2000.
They have: 135 posts
Joined: Apr 2000
Well, that was definitely too easy...!
Psuedocode:
for each in the list of search word(s)
start at beginning of text
search for next match
copy all the text up to that match
add the highlight tag(s), copy the matched searchword, and the end highlight tag
loop to continue the search in the text
next search word
return results
Since I wanted to retain the existing case sensitivity after the highlighting, I couldn't just use a simple Replace function. But it only took 30 minutes or so to get this working (5 to write, 25 to debug ).
In the unlikely case that anyone cares, my actual source code for this is in the file http://www.rewardslookup.com/include/SearchFuncs.inc, and Highlight() is the second function in the file. Go to http://www.RewardsLookup.com/EarnSearch.asp?search=game+toy&srchtype=ANY to see the game and toy words show up in green underline.
Thanks again, Maverick. Obvious though it may be, it wasn't until you suggested the search & replace methodology that I realized how easy this could be...!
[Edited by KLWong on 08-07-2000 at 05:28 PM]
Kristen
http://www.RewardsLookup.com and
http://www.cancerdrugfeedback.net still under early development
Personal page:http://www.isd.net/kwong/kristen.html
Maverick posted this at 17:25 — 8th August 2000.
They have: 334 posts
Joined: Dec 1999
It sounds like you have the same two problems that I always run into.
1) A compelling need to overcomplicate what should be really simple. I wish I had a dollar for every time that I wrote some huge script to accomplish something only to find a 2-line solution a day later
2) The "write for 5, debug for 25" method. Maybe if we took 10 minutes to write in the first place we'd be a lot better off in the long run.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.