I have a simple function that is supposed to highlight keywords and it seems to do fairly well. However, I would like to make it highlight the complete word and not just any portion that was searched but have no idea how to do it as I am weak in regular expressions.
$keywords is space-delimited and $text contains HTML, both of which might be factors. Can someone help? Thanks.
Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
Code:
function Highlight($keywords, $text) {
$StripCommon = array("the", "of", "to", "for");
$keywords = str_replace($StripCommon, "", $keywords);
$keywords = trim($keywords);
if (strlen($keywords)) {
$keywords = explode(" ", $keywords);
if (count($keywords)) {
foreach($keywords as $keyword) {
$text = preg_replace('/(' . $keyword . ')/i', '<span class="HighlightKeywords">\\1</span>', $text);
}
}
}
return $text;
}
$keywords is space-delimited and $text contains HTML, both of which might be factors. Can someone help? Thanks.
Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases