<?
/*  $Id: searchhighlight.php,v 1.5 2004/01/16 11:29:44 pts00065 Exp $
     
     http://www.brambring.nl
     bram-a-t-brambring-d-o-t-nl

    $Log: searchhighlight.php,v $
    Revision 1.5  2004/01/16 11:29:44  pts00065
    Seems that it works now with the case

    Revision 1.4  2004/01/16 10:38:19  pts00065
    The case thing wasn't that easy

    Revision 1.3  2004/01/16 10:09:23  pts00065
    Added:

    vinden.nl
    ilse.nl

    Revision 1.2  2004/01/16 09:37:43  pts00065
    Corrected case

    Revision 1.1  2004/01/16 09:30:14  pts00065
    highlight module


*/
function highlight_capture_start() {
ob_start();
}

function highlight_capture_stop() {
  # 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+site:www.brambring.nl&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) {
  # I don't like this too much, if someone has a better trick;please go ahead
  preg_match_all("/($word)/i",$text,$matched);
  $splitted_text=preg_split ("/$word/i",$text); # explode would be faster but we need the i
  $text="";
  for ( $i=0; $i<count($splitted_text);$i++  ) {
    $text .= $splitted_text[$i];
    $replace=$matched[0][$i];
    if (  preg_match("/<[^>]*$/",$text ) ) { # is the word in a html tag ?
      $text .= $replace;
    } else { 
       $text .="<font class=highlight style=\"background:yellow\">".
                     $replace.
                   "</font>";
    }
  }
}
?>