Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

SearchTerms

Summary: How to get the results of a search to highlight the search terms in a page
Version:
Prerequisites:
Status:
Maintainer:
Categories: Searching

Question

How can I get the results of a search to highlight the search terms in a page?

Answer

The searchterms.phpΔ script highlights any words in the page contents that were used in a search query to arrive at the current page. The script understands queries coming from external search engines such as Google and Yahoo! as well as PmWiki's Search feature.

Discussion

When someone follows a page link coming from a search engine, the query that was used to locate the page is often available to the target site via the HTTP_REFERER variable. This recipe parses HTTP_REFERER to see what words were used in a query, and highlights any instances of those words within the page contents.

As an example, follow this link to have PmWiki search the cookbook for all pages containing the words "search" and "highlight". Then, follow the link from the search results page back to this one, which has the searchterms.phpΔ recipe enabled. You should see all of the instances of "search" and "highlight" displayed with a yellow background on this page.

The color is alterable within this script. scoid

Notes and Comments

This is similar to the SearchHighLight? recipe, except that it doesn't require any modifications to the skin to function. It also allows highlighting of terms in any wiki generated content, not just the main text.

The script currently does not support searches of the form 'group/text' -- the fix is to change $words[$w]++; to $words[preg_replace('/^.*?\\//','',$w)]++;. Also, 'span' is perhaps preferable to 'font'. jr

Is it possible to disable the highlighting inside the sidebar ? jmi

  • Yes, you can override the recipe's style in your CSS file and set the background color to transparent for any sections of your template you don't want highlighted, for example: #wikileft .searchterms { background-color: transparent } RussJackman - September 27, 2010

<<<<<<< It don't work when I search Chinese text.LiGuang?. And also not with the dutch I18n file. -- ~JRoeterd

This ends up clashing with Backlinks. Adding <a href='$PageUrl?action=search&q=link=$FullName'>$[Backlinks]</a> to the template in addition with the this cookbook recipe causes the word link to be highlighted in backlink search results. -cs


This looks great! One question though, I'd like a link occur on the top right side of the page, saying "Remove highlighting". This would be as simple as putting [[{$Name}?action=view]]|Remove Highlighting]] there. How can I add that? I can't find out how to print text whitout using a (:directive:). /blader_se? 2005-07-08

Seems that the highlighting through google search doesn't work anymore...
Elsni October 20, 2005, at 06:57 AM

It's working for me -- I just did a search at [1] and followed the link to this page, and "searchterms" is correctly highlighted. --Pm October 20, 2005, at 07:25 AM
Try again please, I can't see the highlighting in your example, not in mozilla and not in IE. Maybe Google changed something?
Elsni October 27, 2005, at 03:55 AM

Are you sure this recipe is still enabled and working with the current version of PmWiki. I tried the search example link (above) it on two systems (OS X and XP) with two different browsers (Safari and Firefox) and the searched terms were not highlighted). Pico May 30, 2006, at 03:31 PM

It's good. It works on English character, but not work on UNICODE character. what can i do. kksang

See Also

Contributors

  • Pm, 2005-03-05
  • bram
  • fraz
  • Klonk 2005-08-24; fixed a parameters thus this recipe now works also without interfering with BreakPage
Edit - History - Print - Recent Changes - Search
Page last modified on July 07, 2012, at 02:09 PM