Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

SearchHighLight

Summary: Highlight keywords when coming from a search page
Version: 1.16 - 10 July 2005
Prerequisites: Last tested with PmWiki version: 2.0.10
Status:
Maintainer:
Categories: Searching

Goal

Highlight keywords when coming from a search page, PmWiki, Google, etc.

Solution

Based on a nice bit of code found on the internet:

 http://suda.co.uk/projects/SEHL/

The searchhightlight code has been reworked.

Since the code is quite different I removed the old discussion comments and bugs.

Discussion

Usage

Download : searchhighlight_1_16.phpΔ

Install the script in your local or cookbook directory and include it from the farmconfig.php or config.php.

There are three ways to use the script.

The script depends on the refer-header. If for any reason ( firewall, plug-in,configuration) the refer is not send. searchhighlight will hightlight nothing, zip, nada

higtlight everything

Search items found in the entire <body> section are highlighted

$EnableShel = 1;
$ShelPageTextOnly = 0;

Highlight PageText only

Only keywords in the PageText section get highlighted.
This method uses a copy of the HandleBrowse function from pwmiki. If this function is modified the searchhighlightscript needs an update too.

$EnableShel = 1;
$ShelPageTextOnly = 1;

Custom

$EnableShel = 0;
$ShelPageTextOnly = 0;

Use <!--function:sehl_start--> and <!--function:sehl_stop--> in your template to select part to highlight.

Example below highlights PageText and RightBar but ommits SideBar

<!-- function:sehl_start -->
<!-- PageText -->
<!-- function:sehl_stop -->
(:include SideBar:)
<!-- function:sehl_start -->
(:include RightBar:)
<!-- function:sehl_stop -->

Colors

The colors for highlighting are defined in $HTMLStylesFmt['sehl'], use this variable to change the colors or to move the color definition to the stylesheet ($HTMLStylesFmt['sehl']=''')

EnableIMSCaching and other caching

I case the visitor return to a page using a query (s)he should not get a cached page but a new one. The script will turn off IMSCaching in case there is a query string in the refer, ensure the flag is not set on after calling the searchhightlight script.

History

DateVerDescription
07-10-20051.16Rework based on other highlight script
05-03-20051.14Update for pmwiki guidelines
02-26-2004 Update for the templates (0.6.1+)
02-04-20041.11Pmwiki 0.6 rework
  Script requires a style hightlight in a css.
  Figured out how to use lookforward
21-01-20041.6Corrected the highlight of partial matches
16-01-20041.5case insensitve wasn't that easy;sorry.
16-01-20041.2Initial version

Comments & Bugs

  • This recipe seems to clash with sectionedit.php -- 2 November 2007 -- Luigi

Call to undefined function: ob_flush_contents() in \cookbook\searchhighlight.php on line 134

  • this function doesn't exist
  • any hint? ~Isabelle

I had two problems with the existing code:

  1. The function bcmod didn't exist for my installation I used % instead
  2. The regex for comments caused trouble
  • UTF-8/Chinese Words can't work correctly.~LiGuang
    • I think I've found a (quite trivial) solution for this (works for me although YMMV):
      • Go to cookbook/searchhighlight.php:307
      • Comment out $holder = ... and $full_body_hl = ....
      • Uncomment $full_body_hl = ... below what is labeled the ORIGINAL REGEX.
        • Update: it still isn't fully functional. I have added @ in front of preg_replace(...); to hide the error messages and changed the last line of the function to if (strlen($full_body_hl) < strlen($full_body)) return $full_body; else return $full_body_hl; but this recipe needs rewriting.
      • LiGuang, PLEASE link or upload your updated searchhighlight.php ~Kksang
  • Fatal error: Call to undefined function: html_entity_decode() in .../cookbook/searchhighlight_1_16.php on line 258
  • any hint? ~Peter
  • Search words in preformatted blocks are not highlighted (Lorenz).

Slash Bug Fix: When searching for anything containing a forward slash(/), SearchHighLight will trip a preg_replace error on line 307. The line should be as follows:

$holder = preg_replace('/(.*?)('.preg_quote($q, '/' ).')(.*?)/iu',"\$1<span class=\"hl$colour\">\$2</span>\$3",' '.$tag_matches[3][$i].' ');

The seccond paramater to preg_quote() is important. Without it, the forward slash in the search is interpreted as the delimiter to the expression. ~InternetAdvisor

Example

Try a google on kaasfondue+kruiden , my site (brambring.nl) should be in the top:

http://www.google.com&q=kaasfondue+kruiden(approve links)

Or try a wiki search on my page directly:

Contributors

Edit - History - Print - Recent Changes - Search
Page last modified on November 02, 2007, at 10:59 AM