Note: The recipes here are for PmWiki versions 0.6 and 1.0 only. For PmWiki 2.0 recipes, see Cookbook.
To create a new cookbook entry, fill in or remove the sections below.
Goal
Highlight keywords when coming from a search page, pmwiki,google,etc
Solution
Development/ResearchWordHighlighted depicts how to use a bit of javascript to highlight keywords. For some reason it gave an error on my pages. So I created a pmwiki module
Discussion
The basic principle is simple, find and replace. A catch is that we don't want to replace anything in html tags (< >).
For pmwiki with the template system (0.6.1+)
Download and save Attach:searchhighlight_1.13.txt as searchhighlight.php
add to your local/config.php:
-
- include_once("local/searchhighlight.php");
Update your tmpl file (PmWiki.LayoutBasics):
-
- <!--function:highlight_capture_start-->
-
- <!--PageText-->
-
- <!--function:highlight_capture_stop-->
Add a style to your stylesheet:
-
- .highlight {background:yellow}
|
|
|
|
additional comment
This scripts only works unaltered on PHP4 >= 4.3.0 If you're using PHP4 < 4.3.0 insert the following lines to the searchhighlight.php:
|
|
|
|
|
if (!function_exists("ob_get_clean")) {
-
- function ob_get_clean() {
-
- $ob_contents = ob_get_contents();
-
- ob_end_clean();
-
- return $ob_contents;
-
- }
}
|
|
|
|
|
Now this fine block of code works smooth ;-) J.P.Jarolim
|
For pmwiki 0.5
Download and save Attach:searchhighlight_1.7.php
Add the code below to the very end of your local.php (just above the ?>)
-
- if ( ! $action || ($action=='browse' ) ) {
-
- include_once("local/searchhighlight.php");
-
- array_push ($PageHeaderFmt, "function:highlight_capture_start");
-
- array_unshift($PageFooterFmt, "function:highlight_capture_stop");
-
- }
Add a style to the used stylesheet:
-
- .highlight {background:yellow;}
Or edit the script and add some bgcolor to the font (near the end).
See Also
Development/ResearchWordHighlighted
History
| 16-01-2004 | 1.2 | Initial version |
| 16-01-2004 | 1.5 | case insensitve wasn't that easy;sorry. |
| 21-01-2004 | 1.6 | Corrected the highlight of partial matches |
| | | Figured out how to use lookforward |
| | | Script requires a style hightlight in a css. |
| 02-04-2004 | 1.11 | Pmwiki 0.6 rework |
| 02-26-2004 | | Update for the templates (0.6.1+) |
Comments & Bugs
- Using a wrapper around PrintText would be nicer I think
- The highlight function should work for any chunk of text.
The highlight function should work for any chunk of text.
Since SearchWiki finds any chunk of text, they should be highlighted. To get that result, just change the function highlight(..) in the searchhighlight.php to be found above:
|
|
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);
}
|
|
|
function highlight(&$text,$word) {
-
- $word = preg_replace("/#/","\#",$word);
-
- $pregmatch = "#($word)(?![^<]*>)#i";
-
- $pregreplace = "<font class='highlight'>$1</font>";
-
- $text = preg_replace($pregmatch,$pregreplace,$text);
}
|
Now everything gets highlighted. J.P.Jarolim
Contributors
pmwiki-2.2.0-beta68 -- Last modified by {{BrBrBr}}
from IP: 62.59.226.2
ip should be disabled by default for security reasons