Sunday, November 20, 2011

Get the Directories Name by Given Criteria

/*
 * Get the directories name by given criteria.
 *
 * @author    Junaid Atari <mj.atari@gmail.com>
 * @version   1.0
 * @param     string   $dir              Complete path to directory.
 * @param     array    $skipList         Dirs name to be exclude.
 * @param     string   $strictNameRegex  PCRE pattern to filter names
 * @return    array    List of folders name | empty
*/ 
function getDirectryFolders 
   
$dir$skipList = array (), $strictNameRegex '' )
{
    
$listDir = array ();
    
    if ( !
preg_match '@(//|\\\)$@', (string) $dir ) )
        
$dir .= DIRECTORY_SEPARATOR;
    
    
$handler = @opendir $dir );
    
    if ( !
$handler )
        return array ();
    
    
$skipList array_merge ( (array) $skipList, array ( '.''..' ) );
    
    while ( ( 
$sub readdir $handler ) ) !== false )
    { 
        if ( !
in_array $sub$skipList ) && is_dir $dir $sub ) )
        {
            if ( 
trim $strictNameRegex ) )
            {
                if ( (bool) @
preg_match $strictNameRegex$sub ) )
                    
$listDir$sub ] = $dir $sub;
            }
            else
                
$listDir$sub ] = $dir $sub;
        }
    } 
    
    
closedir $handler );
    
    return 
$listDir
}

/*
+---------+
| Example |
+---------+
*/ 
print_r (
    
getDirectryFolders (
        
'c:\htdocs\web',
        array ( 
'includes' ),
        
'/^[a-z0-9_]+$/i'
    
)
);

/*
+--------+
| Output |
+--------+

Array
(
    [backups] => c:\htdocs\web\backups
    [libraries] => c:\htdocs\web\libraries
    [images] => c:\htdocs\web\images
    [logs] => c:\htdocs\web\logs
    [components] => c:\htdocs\web\components
)

*/ 

Get the Files of Directory by Given Criteria

/*
 * 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 $filePATHINFO_FILENAME );
      
        if ( !
in_array $fName, (array) $excludeList ) )
            
$filesList$fName ] = $file;
    }
          
    if ( 
trim $strictNameRegex ) )
    {
        
$fiList = array ();
      
        
$list preg_grep (
            $strictNameRegexarray_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
)

*/ 

Convert BBTags to HTML Span Tag (Recursive)

/*
 * Convert BBTags to HTML span tag and put their name into CSS class.
 *
 * @author    Junaid Atari <mj.atari@gmail.com>
 * @version   1.0
 * @param     string   $str              String to process.
 * @param     bool     $tagsLowerCase    Convert the names in lowercase.
 * @param     bool     $removeEmptyTags  Remove the empty tags.
 * @return    string   Processed text
*/

function replaceTagsToClasses 
   $str$tagsLowerCase false$removeEmptyTags true )
{
    if ( !
is_string $str )
         || !
trim $str ) )
             return 
'';
   
    
/* Regex recursive pattern written by Sag-e-Attar Junaid Atari */
    
preg_match_all '/\[([a-z]+)\](.*|(?R))\[\/\1\]/imsU',
                     $str$matches );
   
    if ( !
count $matches[1] ) )
        return 
$str;
   
    
$total count $matches[1] );
   
    
$list array_map (
        
create_function (
            
'$a,$b,$lc,$rmt''return $rmt && !trim($b) ? "" :
            "<span class=\"".($lc ? strtolower($a) : $a)."\">".
            trim($b)."</span>";'
        ),
        
$matches[1], $matches[2],
        array_fill 0$total$tagsLowerCase ),
        
array_fill 0$total$removeEmptyTags )
    );
   
    return 
replaceTagsToClasses (
        
str_replace $matches[0], $list$str ),
        $tagsLowerCase$removeEmptyTags
    
);
}

/*
+---------+
| Example |
+---------+
*/

$subs '[boldA],[/boldA][a][bold][/bold]dsdd[/a][boldA],[/boldA]';
echo 
replaceTagsToClasses $substrue );

/*
+--------+
| Output |
+--------+
<span class="bolda">,</span><span class="a">dsdd</span><span class="bolda">,</span>
*/