- $explodedSearch = explode(' ', trim($search));
- $keys = array('title', 'description', 'url', 'tags');
- $found = true;
- $searchExactPhrase = false;
-
- // Check if we're using double-quotes to search for the exact string
- if ($search[0] == '"' && $search[strlen($search) - 1] == '"') {
- $searchExactPhrase = true;
-
- // Remove the double-quotes as they are not what we search for
- $search = substr($search, 1, -1);
+ $exactRegex = '/"([^"]+)"/';
+ // Retrieve exact search terms.
+ preg_match_all($exactRegex, $search, $exactSearch);
+ $exactSearch = array_values(array_filter($exactSearch[1]));
+
+ // Remove exact search terms to get AND terms search.
+ $explodedSearchAnd = explode(' ', trim(preg_replace($exactRegex, '', $search)));
+ $explodedSearchAnd = array_values(array_filter($explodedSearchAnd));
+
+ // Filter excluding terms and update andSearch.
+ $excludeSearch = array();
+ $andSearch = array();
+ foreach ($explodedSearchAnd as $needle) {
+ if ($needle[0] == '-' && strlen($needle) > 1) {
+ $excludeSearch[] = substr($needle, 1);
+ } else {
+ $andSearch[] = $needle;
+ }