diff --git a/library/Icinga/Cli/Loader.php b/library/Icinga/Cli/Loader.php index debf52be9..cd376480f 100644 --- a/library/Icinga/Cli/Loader.php +++ b/library/Icinga/Cli/Loader.php @@ -273,30 +273,10 @@ class Loader protected function searchMatch($needle, $haystack) { - $stack = $haystack; - $search = $needle; - $this->lastSuggestions = array(); - while (strlen($search) > 0) { - $len = strlen($search); - foreach ($stack as & $s) { - $s = substr($s, 0, $len); - } - - $res = array_keys($stack, $search, true); - if (count($res) === 1) { - $found = $haystack[$res[0]]; - if (substr($found, 0, strlen($needle)) === $needle) { - return $found; - } else { - return false; - } - } elseif (count($res) > 1) { - foreach ($res as $key) { - $this->lastSuggestions[] = $haystack[$key]; - } - return false; - } - $search = substr($search, 0, -1); + $this->lastSuggestions = preg_grep(sprintf('/^%s.*$/', preg_quote($needle, '/')), $haystack); + $match = array_search($needle, $haystack, true); + if (false !== $match) { + return $haystack[$match]; } return false; }