| Pete M 2004-05-12, 11:59 am |
| I'm using this code from the wiki. Its designed to highlight text like
Google search.
The only problem with it is that its case sensitive.
I'm not an expert by any stretch of the imagination when it comes to
pattern matching. I've read the manual and I still don't know where to
even start !!!
Can someone help please ?
regards
Pedro
function smarty_modifier_highlight ($text, $search)
{
//$text = strtolower($text);
// Init vars
static $_search_phrases;
$i = 0;
$colors = array( '#ffff00','#00ffff','#99ff99','#
ff9999','#ff66ff',
// black on color hilights
'#880000', '#00aa00', '#886800', '#004699', '#990099');
// white on color hilights
// Load saved terms if they have already been parsed.
if (!$_terms = (array)$_search_phrases[$search]) {
// pull out quoted strings
preg_match_all( '/"(.*?)"/', $search, $_quotes);
// split on whitespace
$_terms = array_merge((array)$_quotes[1], explode(' ', preg_replace(
'/".*?"/', ' ', $search )));
$_search_phrases[$search] = $_terms;
}
// Loop through each term and highlight
foreach (array_unique($_terms) as $val) {
// Strip any stray non-matched double quotes and check for empty string
if (!$val = trim(str_replace('"', '', $val)))
continue;
if ($i == 10) $i = 0;
$font_color = ($i > 4) ? 'white': 'black';
//$style = '<span style="color:' . $font_color . ';background-color:' .
$colors[$i++] . '">';
$style = '<span class="highlight">';
// AUTHOR NOTE: this is buggy because if you search for 'style' after
at least one replace,
// the str_replace will find it inside the <b tag. What's needed to fix
this: a regex
// to be used in a preg_replace call that will not replace anything
between <b style...
$text = str_replace($val, "$style$val</span>", $text);
// $text = preg_replace("/($val)/i", $style . '$1' . '</b>', $text);
}
return $text;
}
|