I am using this class to highlight the search keywords on a piece of text:
class highlight
{
public $output_text;
function __construct($text, $words)
{
$split_words = explode( " " , $words );
foreach ($split_words as $word)
{
$text = preg_replace("|($word)|Ui" ,
"<font style=\"background-color:yellow;\"><b>$1</b></font>" , $text );
}
$this->output_text = $text;
}
}
If
$text = "Khalil, M., Paas, F., Johnson, T.E., Su, Y.K., and Payer, A.F. (2008.) Effects of Instructional Strategies Using Cross Sections on the Recognition of Anatomical Structures in Correlated CT and MR Images. <i>
Anatomical Sciences Education, 1(2)</i>
, 75-83 "
which already contains HTML tags, and some of my search keywords are
$words = "Effects color"
The first look will highlight the word Effects, with <font style="background-color:yellow">
Effect</font>
, but the second loop will highlight the word color in the HTML tag. What should I do?
Is it possible to tell preg_replace to only highlight text when its not inside an alligator bracket?