public function highlight($text, $phrase, $format = '<span class="highlight">\\1</span>', $html = false, $regex = "|%s|iu")
{
if (empty($phrase)) {
return $text;
}
if (is_array($phrase)) {
$replace = array();
$with = array();
foreach ($phrase as $key => $segment) {
$segment = '(' . preg_quote($segment, '|') . ')';
if ($html) {
$segment = "(?![^<]+>){$segment}(?![^<]+>)";
}
$with[] = is_array($format) ? $format[$key] : $format;
$replace[] = sprintf($regex, $segment);
}
return preg_replace($replace, $with, $text);
}
$phrase = '(' . preg_quote($phrase, '|') . ')';
if ($html) {
$phrase = "(?![^<]+>){$phrase}(?![^<]+>)";
}
return preg_replace(sprintf($regex, $phrase), $format, $text);
}