function relevanssi_highlight_terms($excerpt, $query) { $type = get_option("relevanssi_highlight"); if ("none" == $type) { return $excerpt; } switch ($type) { case "mark": // thanks to Jeff Byrnes $start_emp = "<mark>"; $end_emp = "</mark>"; break; case "strong": $start_emp = "<strong>"; $end_emp = "</strong>"; break; case "em": $start_emp = "<em>"; $end_emp = "</em>"; break; case "col": $col = get_option("relevanssi_txt_col"); if (!$col) { $col = "#ff0000"; } $start_emp = "<span style='color: {$col}'>"; $end_emp = "</span>"; break; case "bgcol": $col = get_option("relevanssi_bg_col"); if (!$col) { $col = "#ff0000"; } $start_emp = "<span style='background-color: {$col}'>"; $end_emp = "</span>"; break; case "css": $css = get_option("relevanssi_css"); if (!$css) { $css = "color: #ff0000"; } $start_emp = "<span style='{$css}'>"; $end_emp = "</span>"; break; case "class": $css = get_option("relevanssi_class"); if (!$css) { $css = "relevanssi-query-term"; } $start_emp = "<span class='{$css}'>"; $end_emp = "</span>"; break; default: return $excerpt; } $start_emp_token = "**["; $end_emp_token = "]**"; if (function_exists('mb_internal_encoding')) { mb_internal_encoding("UTF-8"); } $terms = array_keys(relevanssi_tokenize($query, $remove_stopwords = true, $min_word_length = -1)); if (is_array($query)) { $query = implode(' ', $query); } // just in case $phrases = relevanssi_extract_phrases(stripslashes($query)); $non_phrase_terms = array(); foreach ($phrases as $phrase) { $phrase_terms = array_keys(relevanssi_tokenize($phrase, $remove_stopwords = false)); foreach ($terms as $term) { if (!in_array($term, $phrase_terms)) { $non_phrase_terms[] = $term; } } $terms = $non_phrase_terms; $terms[] = $phrase; } uksort($terms, 'relevanssi_strlen_sort'); get_option('relevanssi_word_boundaries', 'on') == 'on' ? $word_boundaries = true : ($word_boundaries = false); foreach ($terms as $term) { $pr_term = relevanssi_replace_punctuation(preg_quote($term, '/')); if ($word_boundaries) { $excerpt = preg_replace("/(\\b{$pr_term}|{$pr_term}\\b)(?!(^&+)?(;))/iu", $start_emp_token . '\\1' . $end_emp_token, $excerpt); } else { $excerpt = preg_replace("/({$pr_term})(?!(^&+)?(;))/iu", $start_emp_token . '\\1' . $end_emp_token, $excerpt); } $preg_start = preg_quote($start_emp_token); $preg_end = preg_quote($end_emp_token); if (preg_match_all('/<.*>/U', $excerpt, $matches) > 0) { // Remove highlights from inside HTML tags foreach ($matches as $match) { $new_match = str_replace($start_emp_token, '', $match); $new_match = str_replace($end_emp_token, '', $new_match); $excerpt = str_replace($match, $new_match, $excerpt); } } if (preg_match_all('/<(style|script|object|embed)>.*<\\/(style|script|object|embed)>/U', $excerpt, $matches) > 0) { // Remove highlights in style, object, embed and script tags foreach ($matches as $match) { $new_match = str_replace($start_emp_token, '', $match); $new_match = str_replace($end_emp_token, '', $new_match); $excerpt = str_replace($match, $new_match, $excerpt); } } } $excerpt = relevanssi_remove_nested_highlights($excerpt, $start_emp_token, $end_emp_token); /* $excerpt = htmlentities($excerpt, ENT_QUOTES, 'UTF-8'); // return the HTML entities that were stripped before */ $excerpt = str_replace($start_emp_token, $start_emp, $excerpt); $excerpt = str_replace($end_emp_token, $end_emp, $excerpt); $excerpt = str_replace($end_emp . $start_emp, "", $excerpt); if (function_exists('mb_ereg_replace')) { $pattern = $end_emp . '\\s*' . $start_emp; $excerpt = mb_ereg_replace($pattern, " ", $excerpt); } return $excerpt; }
function relevanssi_highlight_terms($excerpt, $query) { $type = get_option("relevanssi_highlight"); if ("none" == $type) { return $excerpt; } switch ($type) { case "mark": // thanks to Jeff Byrnes $start_emp = "<mark>"; $end_emp = "</mark>"; break; case "strong": $start_emp = "<strong>"; $end_emp = "</strong>"; break; case "em": $start_emp = "<em>"; $end_emp = "</em>"; break; case "col": $col = get_option("relevanssi_txt_col"); if (!$col) $col = "#ff0000"; $start_emp = "<span style='color: $col'>"; $end_emp = "</span>"; break; case "bgcol": $col = get_option("relevanssi_bg_col"); if (!$col) $col = "#ff0000"; $start_emp = "<span style='background-color: $col'>"; $end_emp = "</span>"; break; case "css": $css = get_option("relevanssi_css"); if (!$css) $css = "color: #ff0000"; $start_emp = "<span style='$css'>"; $end_emp = "</span>"; break; case "class": $css = get_option("relevanssi_class"); if (!$css) $css = "relevanssi-query-term"; $start_emp = "<span class='$css'>"; $end_emp = "</span>"; break; default: return $excerpt; } $start_emp_token = "*[/"; $end_emp_token = "\]*"; mb_internal_encoding("UTF-8"); $terms = array_keys(relevanssi_tokenize($query, false)); $phrases = relevanssi_extract_phrases(stripslashes($query)); $non_phrase_terms = array(); foreach ($phrases as $phrase) { $phrase_terms = array_keys(relevanssi_tokenize($phrase, false)); foreach ($terms as $term) { if (!in_array($term, $phrase_terms)) { $non_phrase_terms[] = $term; } } $terms = $non_phrase_terms; $terms[] = $phrase; } usort($terms, 'relevanssi_strlen_sort'); foreach ($terms as $term) { $excerpt = preg_replace("/(\b$term|$term\b)(?!([^<]+)?>)/iu", $start_emp_token . '\\1' . $end_emp_token, $excerpt); // thanks to http://pureform.wordpress.com/2008/01/04/matching-a-word-characters-outside-of-html-tags/ } $excerpt = relevanssi_remove_nested_highlights($excerpt, $start_emp_token, $end_emp_token); $excerpt = str_replace($start_emp_token, $start_emp, $excerpt); $excerpt = str_replace($end_emp_token, $end_emp, $excerpt); $excerpt = str_replace($end_emp . $start_emp, "", $excerpt); if (function_exists('mb_ereg_replace')) { $pattern = $end_emp . '\s*' . $start_emp; $excerpt = mb_ereg_replace($pattern, " ", $excerpt); } return $excerpt; }