From 3ae8de6ca798fb80b93bef3ec03e29c7ed43fbd2 Mon Sep 17 00:00:00 2001 From: brettp Date: Tue, 10 Nov 2009 04:39:32 +0000 Subject: Sorting search contexts by longest related substring. Added more pretty highlight colors. git-svn-id: http://code.elgg.org/elgg/trunk@3659 36083f99-b078-4883-b0ff-0f9b5a30f544 --- mod/search/start.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'mod/search/start.php') diff --git a/mod/search/start.php b/mod/search/start.php index 19de06dd7..6412af8f4 100644 --- a/mod/search/start.php +++ b/mod/search/start.php @@ -239,7 +239,7 @@ function search_get_highlighted_relevant_substrings($haystack, $needle, $min_mat $add_length = floor((($max_length - $total_len) / count($offsets)) / 2); } - + $lengths = array(); foreach ($offsets as $i => $offset) { $limit = $limits[$i]; if ($offset == 0 && $add_length) { @@ -258,19 +258,27 @@ function search_get_highlighted_relevant_substrings($haystack, $needle, $min_mat } $substrings[] = $string; + $lengths[] = strlen($string); } + // sort by length of context. + asort($lengths); + $matched = ''; - foreach ($substrings as $string) { + foreach ($lengths as $i => $len) { + $string = $substrings[$i]; + if (strlen($matched) + strlen($string) < $max_length) { $matched .= $string; } } + $i = 1; foreach ($words as $word) { $search = "/($word)/i"; - $replace = "$1"; + $replace = "$1"; $matched = preg_replace($search, $replace, $matched); + $i++; } return $matched; -- cgit v1.2.3