diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 0b37bf096e2..2bbf9d8d03c 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -11882,6 +11882,24 @@ function dol_getmypid() } +/** + * Generate natural SQL search string for a criteria (this criteria can be tested on one or several fields) + * + * @param string $input String to explode + * @return array Array of string values + */ +function dolExplodeKeepIfQuotes($input) +{ + // Use regexp to capture words and section in quotes + $matches = array(); + preg_match_all('/"([^"]*)"|\'([^\']*)\'|(\S+)/', $input, $matches); + + // Merge result and delete empty values + return array_filter(array_map(function ($a, $b, $c) { + return $a ?: ($b ?: $c); + }, $matches[1], $matches[2], $matches[3])); +} + /** * Generate natural SQL search string for a criteria (this criteria can be tested on one or several fields) * @@ -11918,7 +11936,9 @@ function natural_search($fields, $value, $mode = 0, $nofirstand = 0) $value = preg_replace('/\s*\|\s*/', '|', $value); - $crits = explode(' ', $value); + // Split criteria on ' ' but not if we are inside quotes + $crits = dolExplodeKeepIfQuotes($value); + $res = ''; if (!is_array($fields)) { $fields = array($fields);