I think that you engage in some imaginative but rather unnecessary use of strrchr() and strlen() 🙂
This is my improved version, keeping to your central ideas:
function dirdir($pattern) {
$cwd = strtr(getcwd(), '\\', '/');
$pattern = strtr($pattern, '\\', '/');
if (($pos = strrpos($pattern, '/')) !== false) {
$dir = substr($pattern, 0, $pos + 1);
$file_pattern = substr($pattern, $pos + 1);
} else {
$dir = $cwd . '/';
$file_pattern = $pattern;
}
$files = array();
chdir($dir);
foreach (glob($file_pattern) as $file) {
if (is_file($dir . $file))
$files[] = $file;
}
chdir($cwd);
sort($files);
return $files;
}
This thread probably belongs to the code critique forum, actually.
It may be reasonable to consider the use of natsort() or natcasesort() instead of just sort().