ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
WildcardMatch.php
Go to the documentation of this file.
1 <?php
2 
4 
6 {
7  private const SEARCH_SET = [
8  '/(?<!~)\*/ui',
9  '/~\*/ui',
10  '/(?<!~)\?/ui',
11  '/~\?/ui',
12  ];
13 
14  private const REPLACEMENT_SET = [
15  '${1}.*',
16  '\*',
17  '${1}.',
18  '\?',
19  ];
20 
21  public static function wildcard(string $wildcard): string
22  {
23  // Preg Escape the wildcard, but protecting the Excel * and ? search characters
24  $wildcard = str_replace(['*', '?'], [0x1A, 0x1B], $wildcard);
25  $wildcard = preg_quote($wildcard);
26  $wildcard = str_replace([0x1A, 0x1B], ['*', '?'], $wildcard);
27 
28  return preg_replace(self::SEARCH_SET, self::REPLACEMENT_SET, $wildcard);
29  }
30 
31  public static function compare($value, string $wildcard): bool
32  {
33  if ($value === '') {
34  return true;
35  }
36 
37  return (bool) preg_match("/^{$wildcard}\$/mui", $value);
38  }
39 }