/**
* usage:
*
* searchFiles('someFile.+');
*
* when you want to search case sensitive you have to write
*
* searchFiles('someFile.+', true);
*
* or with an root dir and not case sensitive
*
* searchFiles('someFiles+', false, './some/folder/');
*
* + = there must be something
* * = there can be something
*
* if you want to search for * or + you have to escape them
* \\* or \\+
*
* there are some special characters like (){}[].-*+?$^! and some more which have to be escaped to ^^
*/
function searchFiles($search, $case = false, $dir='./', $parse = true) {
if(!file_exists($dir) || !is_dir($dir)) {
throw new Exception('Der angegebene Ordner existiert nicht oder ist kein Ordner.');
}
$lastChar = substr($dir, -1);
if($lastChar != '/' && $lastChar != '\\') {
$dir .= '/';
}
if($parse) {
$search = preg_replace('/(?!\\\)(\*|\+)/', '.$2', $search);
}
$list = array();
$searchString = '';
$h = opendir($dir);
while(($file = readdir($h)) !== false) {
if($file == '.' || $file == '..') {
continue;
}
$searchString .= $file . "\n";
if(is_dir($dir . $file) && !is_link($dir . $file) && is_readable($dir . $file)) {
$erg = searchFiles($search, $case, $dir . $file . '/', false);
if(count($erg)) {
$list = array_merge($list, $erg);
}
}
}
$matches = array();
if(preg_match_all('/^(.*)'.$search.'(.*)$/'.($case ? 'i' : ''), $searchString, $matches)) {
$list = array_merge($list, $matches[0]);
}
return $list;
}