public function test1() { $this->assertSame(arg_match('file123.jpg', array('file*.jpg')), true); $this->assertSame(arg_match('test-filename.jpg', array('test-*')), true); $this->assertSame(arg_match('test-filename.jpg', array('test-*.jpg')), true); $this->assertSame(arg_match('filname ending with.php', array('*.php')), true); $this->assertSame(arg_match('file.jpg', array('*.gif', '*.jpg')), true); $this->assertSame(arg_match('file.bmp', array('*.gif', '*.jpg')), false); }
/** * Expands a input argument to a file of lists matching certain extensions * * @param $in input argument (full path to file/directory, array with multiple entries) * @param $haystack array describing allowed strings, eg: test*.php, *.jpg, test*, *test * @return array with filenames */ function expand_arg_files($in, $haystack = array()) { if (is_array($in)) { throw new \Exception('FIXME be recursive'); /* $res = array(); foreach ($in as $f) if (in_array( file_suffix($f), $haystack)) $res[] = $f; return $res;*/ } if (is_file($in)) { if (arg_match($in, $haystack)) { return array(realpath($in)); } else { return array(); } } if (is_dir($in)) { return dir_get_matches($in, $haystack, '', true, false); } /* if (strpos($in, "\n") !== false) { if ($haystack) throw new \Exception ('XXX respect $haystack'); return explode("\n", trim($in)); // newline-separated list of filenames with full path }*/ // expand from $in = "/media/downloads/part-of-name*.avi" if (is_string($in) && !$haystack) { return dir_get_matches(dirname($in), array(basename($in))); } throw new \Exception('Unknown input: ' . $in); }