Jump to content

highlighting search terms


michaellunsford

Recommended Posts

There are a few websites out there that highlight the search terms you used to find them. I googled it and found [url=http://www.holovaty.com/blog/archive/2002/08/13/0040]this guy[/url]. It reads like a bragging blog, but he conveniently leaves out the "how to". Likewise, other "javascript" versions I found in google didn't highlight any of my search terms.

What I want to do is for internal MySQL searches, so I'll be looking at $_POST variables, but the idea is the same. I'd like to highlight any $_POSTed search terms.
Link to comment
Share on other sites

If it's for internal, I would simply create a Highlighter object that would be able to be initiated on each page with a list of words. As the page content is generated, run a Parse() function in your Highlighter on the text, doing a str_replace() with the search terms, and surrounding those words with a span that has your highlighting class in your CSS applied to it. Once that's done, when your page is printed out, those matching words will be highlighted appropriately.

Now, as to a javascript version, it would work the same way, except you would simply want to loop through all your tags, running the parsing function on the innerHTML of each one as you went.
Link to comment
Share on other sites

PS> the site that does it...

[url=http://www.google.com/search?client=safari&rls=en&q=htaccess+mod+rewrite+options&ie=UTF-8&oe=UTF-8]Google Search[/url]
top result is [url=http://corz.org/serv/tricks/htaccess2.php].htaccess tips and tricks[/url]

looks like javascript is rewriting theirs -- but my experiments with PHP are working wonderfully.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.