/*
* Get the files of directory by given criteria.
*
* @author Junaid Atari <mj.atari@gmail.com>
* @version 1.0
* @param string $dir Path of directory.
* @param string $pattern Pattern of file name & extesion
* @param array $excludeList Files list to exclude (name only)
* @param string $strictNameRegex PCRE pattern to filter files name
* @return array List of folder's files | empty
*/
function getFolderFiles (
$dir, $pattern = NULL, array $excludeList = array (),
$strictNameRegex = '' )
{
$pattern = !is_string ( $pattern )
? '*.php'
: $pattern;
if ( !preg_match ( '@(//|\\\)$@', $dir ) )
$dir .= DIRECTORY_SEPARATOR;
$_files = glob ( $dir .DIRECTORY_SEPARATOR . $pattern );
if ( !count ( $_files ) )
return array ();
$filesList = array ();
foreach ( $_files as $file )
{
$fName = pathinfo ( $file, PATHINFO_FILENAME );
if ( !in_array ( $fName, (array) $excludeList ) )
$filesList[ $fName ] = $file;
}
if ( trim ( $strictNameRegex ) )
{
$fiList = array ();
$list = preg_grep (
$strictNameRegex, array_keys ( $filesList )
);
if ( !count ( $list ) )
return array ();
foreach ( $list as $name )
{
if ( array_key_exists ( $name, $filesList ) )
$fiList[ $name ] = $filesList[$name];
}
$filesList = $fiList;
}
return $filesList;
}
/*
+---------+
| Example |
+---------+
*/
print_r (
getFolderFiles (
'C:/path/to',
'CF*.php',
array ( 'CTag' ),
'/^CF[a-zA-Z0-9_]+$/'
)
);
/*
+--------+
| Output |
+--------+
Array
(
[CF_Comment] => C:/path/to/CF_Comment.php
[CF_BBTags] => C:/path/to/CF_BBTags.php
[CF_JavaScript] => C:/path/to/CF_JavaScript.php
)
*/
* Get the files of directory by given criteria.
*
* @author Junaid Atari <mj.atari@gmail.com>
* @version 1.0
* @param string $dir Path of directory.
* @param string $pattern Pattern of file name & extesion
* @param array $excludeList Files list to exclude (name only)
* @param string $strictNameRegex PCRE pattern to filter files name
* @return array List of folder's files | empty
*/
function getFolderFiles (
$dir, $pattern = NULL, array $excludeList = array (),
$strictNameRegex = '' )
{
$pattern = !is_string ( $pattern )
? '*.php'
: $pattern;
if ( !preg_match ( '@(//|\\\)$@', $dir ) )
$dir .= DIRECTORY_SEPARATOR;
$_files = glob ( $dir .DIRECTORY_SEPARATOR . $pattern );
if ( !count ( $_files ) )
return array ();
$filesList = array ();
foreach ( $_files as $file )
{
$fName = pathinfo ( $file, PATHINFO_FILENAME );
if ( !in_array ( $fName, (array) $excludeList ) )
$filesList[ $fName ] = $file;
}
if ( trim ( $strictNameRegex ) )
{
$fiList = array ();
$list = preg_grep (
$strictNameRegex, array_keys ( $filesList )
);
if ( !count ( $list ) )
return array ();
foreach ( $list as $name )
{
if ( array_key_exists ( $name, $filesList ) )
$fiList[ $name ] = $filesList[$name];
}
$filesList = $fiList;
}
return $filesList;
}
/*
+---------+
| Example |
+---------+
*/
print_r (
getFolderFiles (
'C:/path/to',
'CF*.php',
array ( 'CTag' ),
'/^CF[a-zA-Z0-9_]+$/'
)
);
/*
+--------+
| Output |
+--------+
Array
(
[CF_Comment] => C:/path/to/CF_Comment.php
[CF_BBTags] => C:/path/to/CF_BBTags.php
[CF_JavaScript] => C:/path/to/CF_JavaScript.php
)
*/
0 comments:
Post a Comment