Skip to content

Instantly share code, notes, and snippets.

@zhester
Created August 13, 2014 16:17
Show Gist options
  • Save zhester/d12c3d9d6e11a683795e to your computer and use it in GitHub Desktop.
Save zhester/d12c3d9d6e11a683795e to your computer and use it in GitHub Desktop.
Another directory listing function that uses a regular expression as a filter.
/**
* Returns a list of all file nodes for a given path.
*
* @param path The path for which to build a list of file node entries.
* @param pattern Optional filter pattern to test each node name against.
* The default pattern prevents returning hidden node entries.
* @return A list of node entries in the given path.
*/
function get_dlist( $path, $pattern = '/^[^.]/' ) {
$nodes = [];
$dh = opendir( $path );
if( $dh ) {
while( ( $node = readdir( $dh ) ) !== false ) {
if( preg_match( $pattern, $node ) ) {
$nodes[] = $node;
}
}
closedir( $dh );
sort( $nodes );
}
return $nodes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment