public static function matchPath(string $pattern, string $path, bool $isCaseSensitive = true): bool
  {
    // Explicitly exclude directory itself.
    if ($path=='' && $pattern=='**/*')
    {
      return false;
    }

    $dirSep         = preg_quote(DIRECTORY_SEPARATOR, '/');
    $trailingDirSep = '(('.$dirSep.')?|('.$dirSep.').+)';

    $patternReplacements = [$dirSep.'\*\*'.$dirSep => $dirSep.'.*'.$trailingDirSep,
                            $dirSep.'\*\*'         => $trailingDirSep,
                            '\*\*'.$dirSep         => '(.*'.$dirSep.')?',
                            '\*\*'                 => '.*',
                            '\*'                   => '[^'.$dirSep.']*',
                            '\?'                   => '[^'.$dirSep.']'];

    $rePattern = preg_quote($pattern, '/');
    $rePattern = str_replace(array_keys($patternReplacements), array_values($patternReplacements), $rePattern);
    $rePattern = '/^'.$rePattern.'$/'.($isCaseSensitive ? '' : 'i');

    return (bool)preg_match($rePattern, $path);
  }
public static function matchPath(string $pattern, string $path, bool $isCaseSensitive = true): bool
  {
    // Explicitly exclude directory itself.
    if ($path=='' && $pattern=='**/*')
    {
      return false;
    }

    $dirSep         = preg_quote(DIRECTORY_SEPARATOR, '/');
    $trailingDirSep = '(('.$dirSep.')?|('.$dirSep.').+)';

    $patternReplacements = [$dirSep.'\*\*'.$dirSep => $dirSep.'.*'.$trailingDirSep,
                            $dirSep.'\*\*'         => $trailingDirSep,
                            '\*\*'.$dirSep         => '(.*'.$dirSep.')?',
                            '\*\*'                 => '.*',
                            '\*'                   => '[^'.$dirSep.']*',
                            '\?'                   => '[^'.$dirSep.']'];

    $rePattern = preg_quote($pattern, '/');
    $rePattern = str_replace(array_keys($patternReplacements), array_values($patternReplacements), $rePattern);
    $rePattern = '/^'.$rePattern.'$/'.($isCaseSensitive ? '' : 'i');

    return (bool)preg_match($rePattern, $path);
  }
public static function matchPath ( string $ pattern , string $ path , bool $ isCaseSensitive = true ) : bool { // Explicitly exclude directory itself. if ( $ path == '' && $ pattern == '**/*' ) { return false ; } $ dirSep = preg_quote ( DIRECTORY_SEPARATOR , '/' ) ; $ trailingDirSep = '((' . $ dirSep . ')?|(' . $ dirSep . ').+)' ; $ patternReplacements = [ $ dirSep . '\*\*' . $ dirSep => $ dirSep . '.*' . $ trailingDirSep , $ dirSep . '\*\*' => $ trailingDirSep , '\*\*' . $ dirSep => '(.*' . $ dirSep . ')?' , '\*\*' => '.*' , '\*' => '[^' . $ dirSep . ']*' , '\?' => '[^' . $ dirSep . ']' ] ; $ rePattern = preg_quote ( $ pattern , '/' ) ; $ rePattern = str_replace ( array_keys ( $ patternReplacements ) , array_values ( $ patternReplacements ) , $ rePattern ) ; $ rePattern = '/^' . $ rePattern . '$/' . ( $ isCaseSensitive ? '' : 'i' ) ; return ( bool ) preg_match ( $ rePattern , $ path ) ; }
Tests whether or not a given path matches a given pattern.

(This method is heavily inspired by SelectorUtils::matchPath from phing.)

@param string $pattern         The pattern to match against.
@param string $path            The path to match.
@param bool   $isCaseSensitive Whether or not matching should be performed case sensitively.

@return bool
Tests whether or not a given path matches a given pattern .