Just use str_replace() to swap out the word with the same word wrapped in a tag that uses CSS to change the background color of the word. Not very hard to do at all.
stymiee
He's No Good To Me Dead
3,360 posts since May 2006
Reputation Points: 161
Solved Threads: 38
<?php
$string = 'But i am not looking this simple one. what i am looking here is i searched a keyword (for eg php ). After searching I got some results from the database. if the result has a keyword php, then that all (php) word should be highlited. please help me';
$search_str = 'php';
function str_highlight ($string, $search_str) {
if (trim($string) == '') {
$return_str = 'Empty string';
}
elseif (trim($search_str) == '') {
$return_str = 'Empty search string';
}
else {
// i put style="...." because i don't have a class="highlight" defined
$return_str = preg_replace ("/$search_str/i",'<span class="highlight" style="background:#f00; color:#fff; font-weight:bold; padding:0 2px;">'.$search_str.'</span>',$string);
}
return $return_str;
}
$new_str = str_highlight ($string, $search_str);
echo $new_str;
?>
johny_d
Junior Poster in Training
94 posts since May 2007
Reputation Points: 33
Solved Threads: 7