public function str_highlight($text, $needle = '', $options = null, $highlight = null) {
$STR_HIGHLIGHT_SIMPLE = 1;
$STR_HIGHLIGHT_WHOLEWD = 2;
$STR_HIGHLIGHT_CASESENS = 0;
$STR_HIGHLIGHT_STRIPLINKS = 8;
if (!$needle) {
return $text;
}
// Default highlighting
if ($highlight === null) {
$highlight = '<strong class="highlight">1</strong>';
}
// Select pattern to use
if ($options & $STR_HIGHLIGHT_SIMPLE) {
$pattern = '#(%s)#';
} else {
$pattern = '#(?!<.*?)(%s)(?![^<>]*?>)#';
$sl_pattern = '#<as(?:.*?)>(%s)</a>#';
}
// Case sensitivity
/*
if ($options ^ $STR_HIGHLIGHT_CASESENS) {
$pattern .= 'i';
$sl_pattern .= 'i';
}
*/
$pattern .= 'i';
$sl_pattern .= 'i';
$needle = (array)$needle;
if (is_array($needle) && count($needle)) {
foreach ($needle as $needle_s) {
$needle_s = preg_quote($needle_s);
// Escape needle with optional whole word check
if ($options & $STR_HIGHLIGHT_WHOLEWD) {
$needle_s = 'b' . $needle_s . 'b';
}
// Strip links
if ($options & $STR_HIGHLIGHT_STRIPLINKS) {
$sl_regex = sprintf($sl_pattern, $needle_s);
$text = preg_replace($sl_regex, '1', $text);
}
$regex = sprintf($pattern, $needle_s);
$text = preg_replace($regex, $highlight, $text);
}
}
return $text;
}