There’s a nice generalised text highlighter on the Smarty Plugins Wiki page so I modified it slightly to highlight b2 searches! Here’s a search for smarty that shows off this new plugin.
How do you use it? Modify your post.tpl and add the modifier to the_content variable like this:
{$the_content|highlightsearch}
If you want different colours try this:
{$the_content|highlightsearch:”<b style=’color: #000; background-color: #f00;’>”:”</b>”}
Text inside html links is also modified so links may be broken. Is there an easy way around that?
Here’s the code for the modifier. Just place it into a file called “modifier.highlightsearch.php” in your plugins directory!
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* Type: modifier
* Name: highlight
* Version: 0.5
* Date: 2003-03-27
* Author: Pavel Prishivalko, aloner#telephone.ru
* Purpose: Highlight search term in text
* Install: Drop into the plugin directory
*
* Extended To 0.5 By: Alexey Kulikov
* Strips Tags for nice output, allows multiple term for highlight
* Modified and simplified to high light b2 searches by Donncha O Caoimh
* -------------------------------------------------------------
*/
function smarty_modifier_highlightsearch($text, $start_tag='<b style="color: #000; background-color: #ff0;">', $end_tag='</b>')
{
global $s;
if( $s != '' )
{
$term = $s;
$term = preg_quote($term);return preg_replace('/('.$term.')/i', $start_tag.'$1'.$end_tag, $text);
}
else
{
return $text;
}
}?>