<?
/*  $Id: searchhighlight.php,v 1.13 2004/02/28 10:52:51 pts00065 Exp $
     
     http://www.brambring.nl
     bram-a-t-brambring-d-o-t-nl

    $Log: searchhighlight.php,v $
    Revision 1.14  2004/03/05 14:19:00
    Added pmwiki test
    Revision 1.13  2004/02/28 10:52:51  pts00065
    Added how-to comment

    Revision 1.12  2004/02/23 10:15:37  pts00065
    First revision after 0.6.1

    Revision 1.11  2004/02/12 09:12:20  pts00065
    Moved action check into searchhighlight

    Revision 1.10  2004/02/12 06:56:01  pts00065
    Small changes for version 0.6.

## USAGE :    
1. Update your tmpl file (See PmWiki.LayoutBasics): 

<!--function:highlight_capture_start--> 
<!--PageText?--> 
<!--function:highlight_capture_stop--> 

2. Add a style to your stylesheet: 
.highlight {background:yellow} 
   
*/
if (!defined('PmWiki')) exit();

function highlight_capture_start() {
if ( $action && $action != 'browse' ) { return ;}
ob_start();
}

function highlight_capture_stop() {
  if ( $action && $action != 'browse' ) { return ;}
  # q,sa_q google;p yahoo;text pmwiki
  $ref_patterns=array("/^search_for=(.*)/","/^query=(.*)/","/^q=(.*)/","/^as_q=(.*)/","/^p=(.*)/","/^text=(.*)/"); 
  $text = ob_get_clean();
  $refer=preg_replace("/^.*\?(.*)/","$1",$_SERVER["HTTP_REFERER"]);
  #$refer="q=Kaasfondue+a+site:http://www.brambring.nl#1&num=20&hl=en&lr=lang_nl|lang_en&ie=UTF-8&oe=UTF-8&start=20&sa=N";

  foreach ( explode ("&",$refer) as $qsa ) {
    $words=preg_replace($ref_patterns,"$1",$qsa);
    if ( $words  != $qsa ) {
     $words=preg_replace("/[\+\-\"\']/"," ",urldecode($words));
     foreach ( explode (" ",$words) as $word ) {
       if ( $word ) { # in case there where double spaces
        highlight($text,$word) ;
       }
     }
    }
  }
echo $text;
}

function highlight(&$text,$word) {
  $word=preg_replace("/#/","\#",$word);
  $pregmatch="#(\b)($word)(\b)(?![^<]*>)#i";
  $pregreplace="$1<font class='highlight'>$2</font>$3";
  $text=preg_replace($pregmatch,$pregreplace,$text);
}
?>