Page 1 of 1

Making keywords bold

Posted: Mon Nov 14, 2005 5:00 pm
by Luke
OK, I came up with this to bold the searched keywords in a database search...

Code: Select all

function highlight($row, $keywords){
		foreach($row as $key => $val){
			$highlight = "<b>$keywords</b>";
			$row[$key] = str_ireplace($keywords, $highlight, $val);
		}
		return $row;
	}
It finds the words regardless of their case, but if the original word is capitalized... it replaces it with lower case words... how do I fix this??

Posted: Mon Nov 14, 2005 5:51 pm
by feyd
regex..

Code: Select all

function highlight($row, $keywords){
        foreach($row as $key => $val){
            $row[$key] = preg_replace('#\b('.implode('|',array_map(createfunction('$a','return preg_quote($a,\'#\');'),$keywords)).')\b#','<b>\1</b>',$val);
        }

Posted: Mon Nov 14, 2005 5:51 pm
by Charles256

Posted: Tue Nov 15, 2005 5:47 am
by twigletmac
In case anyone wonders - preg_*() functions are much better than ereg_*() functions...

Mac

Posted: Tue Nov 15, 2005 3:00 pm
by Ambush Commander
But if you wonder, don't listen to any of us and benchmark it yourself.