Skip to content
Snippets Groups Projects
Commit 26eb848c authored by Gina Haeussge's avatar Gina Haeussge
Browse files

FS#1353: Only highlight isolated occurences of search term, not those where it's part

of another term. Word boundaries are now respected.
parent 92eec8e8
No related branches found
No related tags found
No related merge requests found
......@@ -313,7 +313,7 @@ function ft_snippet($id,$highlight){
$len = utf8_strlen($text);
// build a regexp from the phrases to highlight
$re1 = '('.join('|',array_map('preg_quote_cb',array_filter((array) $highlight))).')';
$re1 = '('.join('|',array_map('_ft_snippet_re_preprocess', array_map('preg_quote_cb',array_filter((array) $highlight)))).')';
$re2 = "$re1.{0,75}(?!\\1)$re1";
$re3 = "$re1.{0,45}(?!\\1)$re1.{0,45}(?!\\1)(?!\\2)$re1";
......@@ -386,6 +386,13 @@ function ft_snippet($id,$highlight){
return $evdata['snippet'];
}
/**
* Wraps a search term in regex boundary checks.
*/
function _ft_snippet_re_preprocess($term) {
return '\b'.$term.'\b';
}
/**
* Combine found documents and sum up their scores
*
......
......@@ -285,13 +285,20 @@ function html_draft(){
*/
function html_hilight($html,$phrases){
$phrases = array_filter((array) $phrases);
$regex = join('|',array_map('preg_quote_cb',$phrases));
$regex = join('|',array_map('_html_hilight_re_preprocess', array_map('preg_quote_cb',$phrases)));
if ($regex === '') return $html;
$html = preg_replace_callback("/((<[^>]*)|$regex)/ui",'html_hilight_callback',$html);
return $html;
}
/**
* Wraps a search term in regex boundary checks.
*/
function _html_hilight_re_preprocess($term) {
return '\b'.$term.'\b';
}
/**
* Callback used by html_hilight()
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment