<?php

echo '<p><b>Demonstrating the power of <a href="preg_find.phps">the preg_find() function</a></b></p>';
include 
'preg_find_2.4.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
// PREG_FIND_FOLLOWSYMLINKS - Recursive searches (from v2.3) will no longer
//                            traverse symlinks to directories, unless you
//                            specify this flag. This is to prevent nasty
//                            endless loops.
//
// You can also request to have the results sorted based on various criteria
// By default if any sorting is done, it will be sorted in ascending order.
// You can reverse this via use of:
// PREG_FIND_SORTDESC    - Reverse order of sort
// PREG_FILE_SORTKEYS    - Sort on the keyvalues or non-assoc array results
// The following sorts *require* PREG_FIND_RETURNASSOC to be used as they are
// sorting on values stored in the constructed associative array
// PREG_FIND_SORTBASENAME - Sort the results in alphabetical order on filename
// PREG_FIND_SORTMODIFIED - Sort the results in last modified timestamp order
// PREG_FIND_SORTFILESIZE     - Sort the results based on filesize
// PREG_FILE_SORTDISKUSAGE    - Sort based on the amount of disk space taken
// PREG_FIND_SORTEXTENSION - Sort based on the filename extension
// to use more than one simple seperate them with a | character


echo <<<EOCODE
<hr><a name='1'>Example 1 listing the files in 'code' directory.
<pre>\$files = preg_find('/./', '../code');
foreach(\$files as \$file) printf("&lt;br>%s\\n", \$file);
</pre>
Output:<code>
EOCODE;
$files preg_find('/./''../code');
foreach(
$files as $fileprintf("<br>%s\n"$file);
echo 
"</code><br>Note that the result is unsorted and in the same order as readdir() would return them.\n";


echo <<<EOCODE
<hr><a name='2'>Example 2 listing the files in 'code' directory, recursively.
<pre>\$files = preg_find('/./', '../code', PREG_FIND_RECURSIVE);
foreach(\$files as \$file) printf("&lt;br>%s\\n", \$file);
</pre>
Output:<code>
EOCODE;
$files preg_find('/./''../code'PREG_FIND_RECURSIVE);
foreach(
$files as $fileprintf("<br>%s\n"$file);
echo 
"</code><br>Note that the result is semi-unsorted. Directories are followed in the order they are met.\n";

echo <<<EOCODE
<hr><a name='3'>Example 3 listing the directories in 'code' directory, recursively.
<pre>\$files = preg_find('/./', '../code', PREG_FIND_DIRONLY|PREG_FIND_RECURSIVE);
foreach(\$files as \$file) printf("&lt;br>%s\\n", \$file);
</pre>
Output:<code>
EOCODE;
$files preg_find('/./''../code'PREG_FIND_DIRONLY|PREG_FIND_RECURSIVE);
foreach(
$files as $fileprintf("<br>%s\n"$file);
echo 
"</code><br>Note that the result is unsorted and in the same order as readdir() would return them.\n";


echo <<<EOCODE
<hr><a name='4'>Example 4 listing the files as per example 1, but this time we
want to only get filenames that match a particular regex: /^str_.*?\.php\$/D
<pre>\$files = preg_find('/^str_.*?\.php\$/D', '../code');
foreach(\$files as \$file) printf("&lt;br>%s\\n", \$file);
</pre>
Output:<code>
EOCODE;
$files preg_find('/^str_.*?\.php$/D''../code');
foreach(
$files as $fileprintf("<br>%s\n"$file);
echo 
"</code><br>Note that the result is unsorted and in the same order as readdir() would return them.\n";


echo <<<EOCODE
<hr><a name='5'>Example 5 listing the files as per example 4, but this time
lets see what the PREG_FIND_RETURNASSOC does to the result
<pre>\$files = preg_find('/^str_.*?\.php$/D', '../code', PREG_FIND_RETURNASSOC);
print_r(\$files);
</pre>
Output:<code><pre>
EOCODE;
$files preg_find('/^str_.*?\.php$/D''../code'PREG_FIND_RETURNASSOC);
print_r($files);
echo 
"</pre></code><br>Quite a change!\n";


echo <<<EOCODE
<hr><a name='6'>Example 6 listing the files as per example 1, but return them
sorted.
<pre>\$files = preg_find('/./', '../code', PREG_FIND_SORTKEYS);
foreach(\$files as \$file) printf("&lt;br>%s\\n", \$file);
</pre>
Output:<code>
EOCODE;
$files preg_find('/./''../code'PREG_FIND_SORTKEYS);
foreach(
$files as $fileprintf("<br>%s\n"$file);
echo 
"</pre></code><br>\n";


echo <<<EOCODE
<hr><a name='7'>Example 7 listing the files as per example 7, but return them
sorted in reverse.
<pre>\$files = preg_find('/./', '../code', PREG_FIND_SORTKEYS|PREG_FIND_SORTDESC);
foreach(\$files as \$file) printf("&lt;br>%s\\n", \$file);
</pre>
Output:<code>
EOCODE;
$files preg_find('/./''../code'PREG_FIND_SORTKEYS|PREG_FIND_SORTDESC);
foreach(
$files as $fileprintf("<br>%s\n"$file);
echo 
"</pre></code><br>\n";


echo <<<EOCODE
<hr><a name='8'>Example 8  listing the largest 5 files in our directory structure.
<pre>
\$files = preg_find('/./', '../code',
  PREG_FIND_RECURSIVE|PREG_FIND_RETURNASSOC|PREG_FIND_SORTFILESIZE|PREG_FIND_SORTDESC);
\$i=1;
foreach(\$files as \$file => \$stats) {
  printf('&lt;br>%d) %d %s', \$i, \$stats['stat']['size'], \$file);
  \$i++;
  if (\$i > 5) break;
}
</pre>
Output:<code>
EOCODE;
$files preg_find('/./''../code',
  
PREG_FIND_RECURSIVE|PREG_FIND_RETURNASSOC|PREG_FIND_SORTFILESIZE|PREG_FIND_SORTDESC);
$i=1;
foreach(
$files as $file => $stats) {
  
printf('<br>%d) %d %s'$i$stats['stat']['size'], $file);
  
$i++;
  if (
$i 5) break;
}
echo 
"</code><br>\n";


echo <<<EOCODE
<hr><a name='9'>Example 9 listing the 10 newest files in our directory structure.
<pre>
\$files = preg_find('/./', '../code',
  PREG_FIND_RECURSIVE|PREG_FIND_RETURNASSOC|PREG_FIND_SORTMODIFIED|PREG_FIND_SORTDESC);
\$i=1;
foreach(\$files as \$file => \$stats) {
  printf('&lt;br>%d) %s - %d bytes - %s', \$i,
    date("Y-m-d H:i:s", \$stats['stat']['mtime']), \$stats['stat']['size'], \$file);
  \$i++;
  if (\$i > 10) break;
}
</pre>
Output:<code>
EOCODE;
$files preg_find('/./''../code',
  
PREG_FIND_RECURSIVE|PREG_FIND_RETURNASSOC|PREG_FIND_SORTMODIFIED|PREG_FIND_SORTDESC);
$i=1;
foreach(
$files as $file => $stats) {
  
printf('<br>%d) %s - %d bytes - %s'$i,
    
date("Y-m-d H:i:s"$stats['stat']['mtime']), $stats['stat']['size'], $file);
  
$i++;
  if (
$i 10) break;
}
echo 
"</code><br>\n";





echo 
'<hr>';


#print '<br><hr>';
#
#$files = preg_find('/./', '.', PREG_FIND_DIRONLY|PREG_FIND_RECURSIVE);
#print '<pre>Matches for all directories: ' . print_r($files, TRUE) . '</pre>';
#
#print '<br><hr>';
#$files = preg_find('/\.phps$/');
#natsort($files);
#print '<pre>Files matching .phps: ' . print_r($files, TRUE) . '</pre>';

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



# Date sorted files - but using built in sorting
print '<br><hr>';
$files preg_find('/./''.'PREG_FIND_RECURSIVE|PREG_FIND_RETURNASSOC|PREG_FIND_SORTMODIFIED|PREG_FIND_SORTDESC);
echo 
'<pre>Files, recursive, in date modified order:<br>';
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>';

# Files in code dir, alphabetically sorted
print '<br><hr>';
$files preg_find('/./''../code'PREG_FIND_RECURSIVE|PREG_FIND_SORTKEYS);
echo 
'<pre>Files in ../code, recursive, in alphabetical order:<br>';
print_r($files);
echo 
'</pre>';

# Files in code dir, alphabetically sorted on BASENAME - note that this
# requires a PREG_FIND_RETURNASSOC argument
print '<br><hr>';
$files preg_find('/./''../code'PREG_FIND_RECURSIVE|PREG_FIND_SORTBASENAME|PREG_FIND_RETURNASSOC);
$files array_keys($files); # we really only want the filenames here
echo '<pre>Files in ../code, recursive, in alphabetical order on file basename:<br>';
print_r($files);
echo 
'</pre>';

# Files in code dir, sorted in reverse file size order.
print '<br><hr>';
$files preg_find('/./''../code'PREG_FIND_RECURSIVE|PREG_FIND_RETURNASSOC|PREG_FIND_SORTFILESIZE|PREG_FIND_SORTDESC);
echo 
'<pre>Files in ../code, recursive, in reverse file size order:<br>';
foreach(
$files as $file => $stats)
  
printf("%s bytes - %s - %s\n"$stats['stat']['size'], date("Y-m-d H:i:s"$stats['stat']['mtime']), $file);
echo 
'</pre>';


?>