Hurray, preg_split did the work... here is the complete code [code] function highlight_words ($text, $searchdata, $bold) { $word_array = explode(" ", $searchdata); $text_pieces = preg_split("'(<.+?>)'", $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); $i = 0; foreach ($text_pieces as $piece) { if (strpos($piece, '<') === FALSE) { foreach ($word_array as $word) { if ($bold == 1) { $piece = preg_replace( "'($word)'si" , "<b style=\"color:blue;\">\\1</b>" , $piece); } else { $piece = preg_replace( "'($word)'si" , "<span style=\"color:blue;\">\\1</span>" , $piece); } } //end foreach $word_array } $new_text_pieces[$i] = $piece; $i++; } //end foreach $text_pieces $text = implode('', $new_text_pieces); return $text; } [/code]