<?php

echo '<p><b>Demonstrating the power of <a href="preg_find.phps">the preg_find() function</a></b></p>';
include 
'preg_find.php';

//optional arguments
// PREG_FIND_RECURSIVE  - go into subdirectorys looking for more files
// PREG_FIND_DIRMATCH   - return directorys that match the pattern also
// PREG_FIND_DIRONLY    - return only directorys that match the pattern
// PREG_FIND_FULLPATH   - search for the pattern in the full path (dir+file)
// PREG_FIND_NEGATE     - return files that don't match the pattern
// PREG_FIND_RETURNASSOC - Instead of just returning a plain array of matches,
//                         return an associative array with file stats
// to use more than one simple seperate them with a | character

$files preg_find("/preg_/"'.'PREG_FIND_RETURNASSOC);

print 
'<pre>Matches for files called "preg_": ' print_r($filesTRUE) . '</pre>';

print 
'<br><hr>';

$files preg_find('/./''.'PREG_FIND_DIRONLY|PREG_FIND_RECURSIVE);
print 
'<pre>Matches for all directories: ' print_r($filesTRUE) . '</pre>';

print 
'<br><hr>';
$files preg_find('/\.phps$/');
natsort($files);
print 
'<pre>Files matching .phps: ' print_r($filesTRUE) . '</pre>';

print 
'<br><hr>';
$files preg_find('/./''./testdir'PREG_FIND_RECURSIVE);
print 
'<pre>Files in testdir, recursive: ' print_r($filesTRUE) . '</pre>';

# Alpha sorted directories
Function cmp_basename_alpha_asc($a$b) {
  
$key 'basename';
  
#print "Comparing: " . print_r($a, true) . " and " . print_r($b,true) . "\n<br>";
  
if ($a[$key] == $b[$key]) return 0;
  return (
$a[$key] < $b[$key]) ? -1;
}
print 
'<br><hr>';
$files preg_find('/./''./testdir'PREG_FIND_DIRONLY|PREG_FIND_RETURNASSOC);
uasort($files'cmp_basename_alpha_asc');
print 
'<pre>Directories, in alphabetical order: ' print_r(array_keys($files), TRUE) . '</pre>';


# Date sorted directories
Function cmp_basename_modified_asc($a$b) {
  
$key1 'stat';
  
$key2 'mtime';
  
#print "Comparing: " . print_r($a, true) . " and " . print_r($b,true) . "\n<br>";
  #if ($a[$key] == $b[$key]) return 0;
  #return ($a[$key] < $b[$key]) ? -1 : 1;
  
if ($a[$key1][$key2] == $b[$key1][$key2]) return 0;
  return (
$a[$key1][$key2] < $b[$key1][$key2]) ? -1;
}
print 
'<br><hr>';
$files preg_find('/./''./testdir'PREG_FIND_DIRONLY|PREG_FIND_RETURNASSOC);
uasort($files'cmp_basename_modified_asc');
print 
'<pre>Directories, in date modified order: ' print_r(array_keys($files), TRUE) . '</pre>';



# Date sorted files
print '<br><hr>';
$files preg_find('/./''.'PREG_FIND_RECURSIVE|PREG_FIND_RETURNASSOC);
uasort($files'cmp_basename_modified_asc');
#print '<pre>Files, recursive, in date modified order: ' . print_r(array_keys($files), TRUE) . '</pre>';
echo '<pre>Files, recursive, in date modified order: ';
foreach(
$files as $file => $stats)
  
printf("%s - %s - %s\n"$stats['stat']['mtime'], date("Y-m-d H:i:s"$stats['stat']['mtime']), $file);
echo 
'</pre>';

?>