diff options
author | Joas Schilling <nickvergessen@gmx.de> | 2014-04-15 23:23:26 +0200 |
---|---|---|
committer | Joas Schilling <nickvergessen@gmx.de> | 2014-04-15 23:23:26 +0200 |
commit | 675cef9c2e9ea571606415fb3228b5f2c73769d9 (patch) | |
tree | 4e47ae4209af12763265d69fddf90ad8b93ed021 /phpBB | |
parent | e64c2326bdf36ac071f07881d4ca5aba202e6249 (diff) | |
parent | f3cd7f73e1ed3acbf1a319331e2f99ea27d5fe2d (diff) | |
download | forums-675cef9c2e9ea571606415fb3228b5f2c73769d9.tar forums-675cef9c2e9ea571606415fb3228b5f2c73769d9.tar.gz forums-675cef9c2e9ea571606415fb3228b5f2c73769d9.tar.bz2 forums-675cef9c2e9ea571606415fb3228b5f2c73769d9.tar.xz forums-675cef9c2e9ea571606415fb3228b5f2c73769d9.zip |
Merge pull request #2195 from Elsensee/ticket/10423
[ticket/10423] Remove * from search or highlight string
* Elsensee/ticket/10423:
[ticket/10423] Replace foreach with function in viewtopic.php
[ticket/10423] Remove unnecessary include in test
[ticket/10423] Match multiple wildcards
[ticket/10423] Move code into a function and add tests for it
[ticket/10423] Remove * from search or highlight string
Diffstat (limited to 'phpBB')
-rw-r--r-- | phpBB/includes/functions_content.php | 18 | ||||
-rw-r--r-- | phpBB/search.php | 9 | ||||
-rw-r--r-- | phpBB/viewtopic.php | 16 |
3 files changed, 28 insertions, 15 deletions
diff --git a/phpBB/includes/functions_content.php b/phpBB/includes/functions_content.php index 6213d2fd24..19459239d5 100644 --- a/phpBB/includes/functions_content.php +++ b/phpBB/includes/functions_content.php @@ -21,6 +21,7 @@ if (!defined('IN_PHPBB')) * make_jumpbox() * bump_topic_allowed() * get_context() +* phpbb_clean_search_string() * decode_message() * strip_bbcode() * generate_text_for_display() @@ -361,6 +362,23 @@ function get_context($text, $words, $length = 400) } /** +* Cleans a search string by removing single wildcards from it and replacing multiple spaces with a single one. +* +* @param string $search_string The full search string which should be cleaned. +* +* @return string The cleaned search string without any wildcards and multiple spaces. +*/ +function phpbb_clean_search_string($search_string) +{ + // This regular expressions matches every single wildcard. + // That means one after a whitespace or the beginning of the string or one before a whitespace or the end of the string. + $search_string = preg_replace('#(?<=^|\s)\*+(?=\s|$)#', '', $search_string); + $search_string = trim($search_string); + $search_string = preg_replace(array('#\s+#u', '#\*+#u'), array(' ', '*'), $search_string); + return $search_string; +} + +/** * Decode text whereby text is coming from the db and expected to be pre-parsed content * We are placing this outside of the message parser because we are often in need of it... */ diff --git a/phpBB/search.php b/phpBB/search.php index 0f13dbbfa0..d0d86fac17 100644 --- a/phpBB/search.php +++ b/phpBB/search.php @@ -574,9 +574,9 @@ if ($keywords || $author || $author_id || $search_id || $submit) } // define some vars for urls - $hilit = implode('|', explode(' ', preg_replace('#\s+#u', ' ', str_replace(array('+', '-', '|', '(', ')', '"'), ' ', $keywords)))); - // Do not allow *only* wildcard being used for hilight - $hilit = (strspn($hilit, '*') === strlen($hilit)) ? '' : $hilit; + // A single wildcard will make the search results look ugly + $hilit = phpbb_clean_search_string(str_replace(array('+', '-', '|', '(', ')', '"'), ' ', $keywords)); + $hilit = str_replace(' ', '|', $hilit); $u_hilit = urlencode(htmlspecialchars_decode(str_replace('|', ' ', $hilit))); $u_show_results = '&sr=' . $show_results; @@ -840,7 +840,8 @@ if ($keywords || $author || $author_id || $search_id || $submit) $hilit_array = array_filter(explode('|', $hilit), 'strlen'); foreach ($hilit_array as $key => $value) { - $hilit_array[$key] = str_replace('\*', '\w*?', preg_quote($value, '#')); + $hilit_array[$key] = phpbb_clean_search_string($value); + $hilit_array[$key] = str_replace('\*', '\w*?', preg_quote($hilit_array[$key], '#')); $hilit_array[$key] = preg_replace('#(^|\s)\\\\w\*\?(\s|$)#', '$1\w+?$2', $hilit_array[$key]); } $hilit = implode('|', $hilit_array); diff --git a/phpBB/viewtopic.php b/phpBB/viewtopic.php index e08d6e1ef5..f52e04e1a8 100644 --- a/phpBB/viewtopic.php +++ b/phpBB/viewtopic.php @@ -471,17 +471,11 @@ else $highlight_match = $highlight = ''; if ($hilit_words) { - foreach (explode(' ', trim($hilit_words)) as $word) - { - if (trim($word)) - { - $word = str_replace('\*', '\w+?', preg_quote($word, '#')); - $word = preg_replace('#(^|\s)\\\\w\*\?(\s|$)#', '$1\w+?$2', $word); - $highlight_match .= (($highlight_match != '') ? '|' : '') . $word; - } - } - - $highlight = urlencode($hilit_words); + $highlight_match = phpbb_clean_search_string($hilit_words); + $highlight = urlencode($highlight_match); + $highlight_match = str_replace('\*', '\w+?', preg_quote($highlight_match, '#')); + $highlight_match = preg_replace('#(?<=^|\s)\\\\w\*\?(?=\s|$)#', '\w+?', $highlight_match); + $highlight_match = str_replace(' ', '|', $highlight_match); } // Make sure $start is set to the last page if it exceeds the amount |