Demonstrating the power of the preg_find() function


Example 1 listing the files in 'code' directory.
$files = preg_find('/./', '../code');
foreach($files as $file) printf("<br>%s\n", $file);
Output:
../code/404.phps
../code/hexstr.phps
../code/reentrant.php
../code/reentrant_f1.php
../code/str_case.phps
../code/str_split.phps
../code/strnpos.phps
../code/wordwrap_bigwords.phps
../code/hexstr.php
../code/404.php
../code/reentrant_f2.php
../code/maillog.phps
../code/runtimer.php
../code/single_astmatch.php
../code/runtimer.phps
../code/number_to_words.php
../code/foo.php
../code/str_case.php
../code/str_split.php
../code/strnpos.php
../code/validate_email.inc.phps
../code/wordwrap_bigwords.php
../code/200501152253
../code/number_to_words_ex.php
../code/number_to_words_ex.phps
../code/number_to_words.phps
../code/number_to_words_apathy.php
../code/argparse.php
../code/showvalidemail.php
../code/code_debug_timing.php
../code/recursive_readdir.php
../code/recursive_readdir.phps
../code/days_of_year.php
../code/str_rotN.php
../code/morse.php
../code/rc4.inc.phps
../code/morse.phps
../code/tail-f.php
../code/tail-f.phps
../code/dirlister.php
../code/dirlister.phps
../code/tail-10.php
../code/tail-10.phps
../code/bcceil.php
../code/ftruncate.php

Note that the result is unsorted and in the same order as readdir() would return them.
Example 2 listing the files in 'code' directory, recursively.
$files = preg_find('/./', '../code', PREG_FIND_RECURSIVE);
foreach($files as $file) printf("<br>%s\n", $file);
Output:
../code/404.phps
../code/hexstr.phps
../code/reentrant.php
../code/reentrant_f1.php
../code/str_case.phps
../code/str_split.phps
../code/strnpos.phps
../code/testdir/testfile1
../code/testdir/dir2/testfile2
../code/testdir/dir2/dir2.2/testfile4
../code/testdir/dir2/dir2.2/subdir/1/2/3/deepfile
../code/testdir/dir3/testfile3
../code/wordwrap_bigwords.phps
../code/hexstr.php
../code/404.php
../code/reentrant_f2.php
../code/maillog.phps
../code/runtimer.php
../code/single_astmatch.php
../code/runtimer.phps
../code/number_to_words.php
../code/foo.php
../code/str_case.php
../code/str_split.php
../code/strnpos.php
../code/validate_email.inc.phps
../code/wordwrap_bigwords.php
../code/200501152253
../code/number_to_words_ex.php
../code/number_to_words_ex.phps
../code/number_to_words.phps
../code/number_to_words_apathy.php
../code/argparse.php
../code/showvalidemail.php
../code/code_debug_timing.php
../code/recursive_readdir.php
../code/recursive_readdir.phps
../code/days_of_year.php
../code/str_rotN.php
../code/morse.php
../code/rc4.inc.phps
../code/morse.phps
../code/tail-f.php
../code/tail-f.phps
../code/dirlister.php
../code/dirlister.phps
../code/tail-10.php
../code/tail-10.phps
../code/bcceil.php
../code/ftruncate.php

Note that the result is semi-unsorted. Directories are followed in the order they are met.
Example 3 listing the directories in 'code' directory, recursively.
$files = preg_find('/./', '../code', PREG_FIND_DIRONLY|PREG_FIND_RECURSIVE);
foreach($files as $file) printf("<br>%s\n", $file);
Output:
../code/testdir
../code/testdir/dir2
../code/testdir/dir2/dir2.2
../code/testdir/dir2/dir2.2/subdir
../code/testdir/dir2/dir2.2/subdir/1
../code/testdir/dir2/dir2.2/subdir/1/2
../code/testdir/dir2/dir2.2/subdir/1/2/3
../code/testdir/dir3

Note that the result is unsorted and in the same order as readdir() would return them.
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
$files = preg_find('/^str_.*?\.php$/D', '../code');
foreach($files as $file) printf("<br>%s\n", $file);
Output:
../code/str_case.php
../code/str_split.php
../code/str_rotN.php

Note that the result is unsorted and in the same order as readdir() would return them.
Example 5 listing the files as per example 4, but this time lets see what the PREG_FIND_RETURNASSOC does to the result
$files = preg_find('/^str_.*?\.php$/D', '../code', PREG_FIND_RETURNASSOC);
print_r($files);
Output:
Array
(
    [../code/str_case.php] => Array
        (
            [stat] => Array
                (
                    [0] => 1043
                    [1] => 2521399
                    [2] => 33188
                    [3] => 1
                    [4] => 0
                    [5] => 0
                    [6] => 10120864
                    [7] => 2903
                    [8] => 1125616106
                    [9] => 1124413021
                    [10] => 1125616106
                    [11] => 16384
                    [12] => 8
                    [dev] => 1043
                    [ino] => 2521399
                    [mode] => 33188
                    [nlink] => 1
                    [uid] => 0
                    [gid] => 0
                    [rdev] => 10120864
                    [size] => 2903
                    [atime] => 1125616106
                    [mtime] => 1124413021
                    [ctime] => 1125616106
                    [blksize] => 16384
                    [blocks] => 8
                )

            [du] => 4096
            [uid] => 0
            [gid] => 0
            [filetype] => file
            [mimetype] => text/plain
            [dirname] => ../code
            [basename] => str_case.php
            [owner] => Array
                (
                    [name] => root
                    [passwd] => *
                    [uid] => 0
                    [gid] => 0
                    [gecos] => Charlie &
                    [dir] => /root
                    [shell] => /bin/csh
                )

        )

    [../code/str_split.php] => Array
        (
            [stat] => Array
                (
                    [0] => 1043
                    [1] => 2521400
                    [2] => 33188
                    [3] => 1
                    [4] => 1001
                    [5] => 0
                    [6] => 10119260
                    [7] => 745
                    [8] => 1125616106
                    [9] => 1154566256
                    [10] => 1154566256
                    [11] => 16384
                    [12] => 4
                    [dev] => 1043
                    [ino] => 2521400
                    [mode] => 33188
                    [nlink] => 1
                    [uid] => 1001
                    [gid] => 0
                    [rdev] => 10119260
                    [size] => 745
                    [atime] => 1125616106
                    [mtime] => 1154566256
                    [ctime] => 1154566256
                    [blksize] => 16384
                    [blocks] => 4
                )

            [du] => 2048
            [uid] => 1001
            [gid] => 0
            [filetype] => file
            [mimetype] => text/plain
            [dirname] => ../code
            [basename] => str_split.php
            [owner] => Array
                (
                    [name] => pgregg
                    [passwd] => *
                    [uid] => 1001
                    [gid] => 1001
                    [gecos] => Paul Gregg
                    [dir] => /home/pgregg
                    [shell] => /usr/local/bin/zsh
                )

        )

    [../code/str_rotN.php] => Array
        (
            [stat] => Array
                (
                    [0] => 1043
                    [1] => 2522881
                    [2] => 33188
                    [3] => 1
                    [4] => 0
                    [5] => 0
                    [6] => 10128343
                    [7] => 652
                    [8] => 1180134307
                    [9] => 1180732295
                    [10] => 1180732295
                    [11] => 16384
                    [12] => 4
                    [dev] => 1043
                    [ino] => 2522881
                    [mode] => 33188
                    [nlink] => 1
                    [uid] => 0
                    [gid] => 0
                    [rdev] => 10128343
                    [size] => 652
                    [atime] => 1180134307
                    [mtime] => 1180732295
                    [ctime] => 1180732295
                    [blksize] => 16384
                    [blocks] => 4
                )

            [du] => 2048
            [uid] => 0
            [gid] => 0
            [filetype] => file
            [mimetype] => text/plain
            [dirname] => ../code
            [basename] => str_rotN.php
            [owner] => Array
                (
                    [name] => root
                    [passwd] => *
                    [uid] => 0
                    [gid] => 0
                    [gecos] => Charlie &
                    [dir] => /root
                    [shell] => /bin/csh
                )

        )

)

Quite a change!
Example 6 listing the files as per example 1, but return them sorted.
$files = preg_find('/./', '../code', PREG_FIND_SORTKEYS);
foreach($files as $file) printf("<br>%s\n", $file);
Output:
../code/200501152253
../code/404.php
../code/404.phps
../code/argparse.php
../code/bcceil.php
../code/code_debug_timing.php
../code/days_of_year.php
../code/dirlister.php
../code/dirlister.phps
../code/foo.php
../code/ftruncate.php
../code/hexstr.php
../code/hexstr.phps
../code/maillog.phps
../code/morse.php
../code/morse.phps
../code/number_to_words.php
../code/number_to_words.phps
../code/number_to_words_apathy.php
../code/number_to_words_ex.php
../code/number_to_words_ex.phps
../code/rc4.inc.phps
../code/recursive_readdir.php
../code/recursive_readdir.phps
../code/reentrant.php
../code/reentrant_f1.php
../code/reentrant_f2.php
../code/runtimer.php
../code/runtimer.phps
../code/showvalidemail.php
../code/single_astmatch.php
../code/str_case.php
../code/str_case.phps
../code/str_rotN.php
../code/str_split.php
../code/str_split.phps
../code/strnpos.php
../code/strnpos.phps
../code/tail-10.php
../code/tail-10.phps
../code/tail-f.php
../code/tail-f.phps
../code/validate_email.inc.phps
../code/wordwrap_bigwords.php
../code/wordwrap_bigwords.phps


Example 7 listing the files as per example 7, but return them sorted in reverse.
$files = preg_find('/./', '../code', PREG_FIND_SORTKEYS|PREG_FIND_SORTDESC);
foreach($files as $file) printf("<br>%s\n", $file);
Output:
../code/wordwrap_bigwords.phps
../code/wordwrap_bigwords.php
../code/validate_email.inc.phps
../code/tail-f.phps
../code/tail-f.php
../code/tail-10.phps
../code/tail-10.php
../code/strnpos.phps
../code/strnpos.php
../code/str_split.phps
../code/str_split.php
../code/str_rotN.php
../code/str_case.phps
../code/str_case.php
../code/single_astmatch.php
../code/showvalidemail.php
../code/runtimer.phps
../code/runtimer.php
../code/reentrant_f2.php
../code/reentrant_f1.php
../code/reentrant.php
../code/recursive_readdir.phps
../code/recursive_readdir.php
../code/rc4.inc.phps
../code/number_to_words_ex.phps
../code/number_to_words_ex.php
../code/number_to_words_apathy.php
../code/number_to_words.phps
../code/number_to_words.php
../code/morse.phps
../code/morse.php
../code/maillog.phps
../code/hexstr.phps
../code/hexstr.php
../code/ftruncate.php
../code/foo.php
../code/dirlister.phps
../code/dirlister.php
../code/days_of_year.php
../code/code_debug_timing.php
../code/bcceil.php
../code/argparse.php
../code/404.phps
../code/404.php
../code/200501152253


Example 8 listing the largest 5 files in our directory structure.
$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;
}
Output:
1) 12947 ../code/showvalidemail.php
2) 5974 ../code/dirlister.php
3) 5974 ../code/dirlister.phps
4) 3193 ../code/404.phps
5) 3193 ../code/404.php


Example 9 listing the 10 newest files in our directory structure.
$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;
}
Output:
1) 2008-05-06 20:09:31 - 681 bytes - ../code/bcceil.php
2) 2008-04-07 20:27:36 - 12947 bytes - ../code/showvalidemail.php
3) 2008-03-10 10:48:13 - 491 bytes - ../code/ftruncate.php
4) 2008-01-11 17:15:31 - 2152 bytes - ../code/tail-10.php
5) 2008-01-11 17:15:31 - 2152 bytes - ../code/tail-10.phps
6) 2008-01-11 16:49:50 - 676 bytes - ../code/tail-f.php
7) 2008-01-11 16:49:50 - 676 bytes - ../code/tail-f.phps
8) 2007-12-07 17:25:02 - 3193 bytes - ../code/404.phps
9) 2007-12-07 17:25:02 - 3193 bytes - ../code/404.php
10) 2007-11-04 23:07:37 - 5974 bytes - ../code/dirlister.phps




Files in testdir, recursive: Array
(
    [0] => ../code/testdir/testfile1
    [1] => ../code/testdir/dir2/testfile2
    [2] => ../code/testdir/dir2/dir2.2/testfile4
    [3] => ../code/testdir/dir2/dir2.2/subdir/1/2/3/deepfile
    [4] => ../code/testdir/dir3/testfile3
)


Files, recursive, in date modified order:
1181420217 - 2007-06-09 21:16:57 - ./preg_find.php.txt 1181420217 - 2007-06-09 21:16:57 - ./preg_find.php 1181420217 - 2007-06-09 21:16:57 - ./preg_find.phps 1181420059 - 2007-06-09 21:14:19 - ./old/2.1/preg_find.php 1176946037 - 2007-04-19 02:27:17 - ./preg_find_ex.phps 1176946037 - 2007-04-19 02:27:17 - ./preg_find_ex.php 1176804073 - 2007-04-17 11:01:13 - ./old/2.0/preg_find_ex.php 1176804073 - 2007-04-17 11:01:13 - ./old/2.0/preg_find_ex.phps 1125275750 - 2005-08-29 01:35:50 - ./old/2.0/preg_find.php 1125275750 - 2005-08-29 01:35:50 - ./old/2.0/preg_find.phps


Files in ../code, recursive, in alphabetical order:
Array ( [27] => ../code/200501152253 [14] => ../code/404.php [0] => ../code/404.phps [32] => ../code/argparse.php [48] => ../code/bcceil.php [34] => ../code/code_debug_timing.php [37] => ../code/days_of_year.php [44] => ../code/dirlister.php [45] => ../code/dirlister.phps [21] => ../code/foo.php [49] => ../code/ftruncate.php [13] => ../code/hexstr.php [1] => ../code/hexstr.phps [16] => ../code/maillog.phps [39] => ../code/morse.php [41] => ../code/morse.phps [20] => ../code/number_to_words.php [30] => ../code/number_to_words.phps [31] => ../code/number_to_words_apathy.php [28] => ../code/number_to_words_ex.php [29] => ../code/number_to_words_ex.phps [40] => ../code/rc4.inc.phps [35] => ../code/recursive_readdir.php [36] => ../code/recursive_readdir.phps [2] => ../code/reentrant.php [3] => ../code/reentrant_f1.php [15] => ../code/reentrant_f2.php [17] => ../code/runtimer.php [19] => ../code/runtimer.phps [33] => ../code/showvalidemail.php [18] => ../code/single_astmatch.php [22] => ../code/str_case.php [4] => ../code/str_case.phps [38] => ../code/str_rotN.php [23] => ../code/str_split.php [5] => ../code/str_split.phps [24] => ../code/strnpos.php [6] => ../code/strnpos.phps [46] => ../code/tail-10.php [47] => ../code/tail-10.phps [42] => ../code/tail-f.php [43] => ../code/tail-f.phps [10] => ../code/testdir/dir2/dir2.2/subdir/1/2/3/deepfile [9] => ../code/testdir/dir2/dir2.2/testfile4 [8] => ../code/testdir/dir2/testfile2 [11] => ../code/testdir/dir3/testfile3 [7] => ../code/testdir/testfile1 [25] => ../code/validate_email.inc.phps [26] => ../code/wordwrap_bigwords.php [12] => ../code/wordwrap_bigwords.phps )


Files in ../code, recursive, in alphabetical order on file basename:
Array ( [0] => ../code/200501152253 [1] => ../code/404.php [2] => ../code/404.phps [3] => ../code/argparse.php [4] => ../code/bcceil.php [5] => ../code/code_debug_timing.php [6] => ../code/days_of_year.php [7] => ../code/testdir/dir2/dir2.2/subdir/1/2/3/deepfile [8] => ../code/dirlister.php [9] => ../code/dirlister.phps [10] => ../code/foo.php [11] => ../code/ftruncate.php [12] => ../code/hexstr.php [13] => ../code/hexstr.phps [14] => ../code/maillog.phps [15] => ../code/morse.php [16] => ../code/morse.phps [17] => ../code/number_to_words.php [18] => ../code/number_to_words.phps [19] => ../code/number_to_words_apathy.php [20] => ../code/number_to_words_ex.php [21] => ../code/number_to_words_ex.phps [22] => ../code/rc4.inc.phps [23] => ../code/recursive_readdir.php [24] => ../code/recursive_readdir.phps [25] => ../code/reentrant.php [26] => ../code/reentrant_f1.php [27] => ../code/reentrant_f2.php [28] => ../code/runtimer.php [29] => ../code/runtimer.phps [30] => ../code/showvalidemail.php [31] => ../code/single_astmatch.php [32] => ../code/str_case.php [33] => ../code/str_case.phps [34] => ../code/str_rotN.php [35] => ../code/str_split.php [36] => ../code/str_split.phps [37] => ../code/strnpos.php [38] => ../code/strnpos.phps [39] => ../code/tail-10.php [40] => ../code/tail-10.phps [41] => ../code/tail-f.php [42] => ../code/tail-f.phps [43] => ../code/testdir/testfile1 [44] => ../code/testdir/dir2/testfile2 [45] => ../code/testdir/dir3/testfile3 [46] => ../code/testdir/dir2/dir2.2/testfile4 [47] => ../code/validate_email.inc.phps [48] => ../code/wordwrap_bigwords.php [49] => ../code/wordwrap_bigwords.phps )


Files in ../code, recursive, in reverse file size order:
12947 bytes - 2008-04-07 20:27:36 - ../code/showvalidemail.php 5974 bytes - 2007-11-04 23:07:37 - ../code/dirlister.php 5974 bytes - 2007-11-04 23:07:37 - ../code/dirlister.phps 3193 bytes - 2007-12-07 17:25:02 - ../code/404.phps 3193 bytes - 2007-12-07 17:25:02 - ../code/404.php 2997 bytes - 2006-05-24 15:22:31 - ../code/number_to_words.php 2997 bytes - 2006-05-24 15:22:31 - ../code/number_to_words.phps 2903 bytes - 2005-08-19 01:57:01 - ../code/str_case.php 2903 bytes - 2005-08-19 01:57:01 - ../code/str_case.phps 2152 bytes - 2008-01-11 17:15:31 - ../code/tail-10.phps 2152 bytes - 2008-01-11 17:15:31 - ../code/tail-10.php 2013 bytes - 2007-10-01 23:58:02 - ../code/code_debug_timing.php 2012 bytes - 2006-05-24 15:33:30 - ../code/number_to_words_apathy.php 1950 bytes - 2007-06-01 23:03:52 - ../code/rc4.inc.phps 1827 bytes - 2007-06-28 20:05:31 - ../code/validate_email.inc.phps 1497 bytes - 2007-06-08 11:50:50 - ../code/morse.php 1497 bytes - 2007-06-08 11:50:50 - ../code/morse.phps 1467 bytes - 2005-08-05 14:10:19 - ../code/maillog.phps 1260 bytes - 2005-08-05 13:32:37 - ../code/strnpos.php 1260 bytes - 2005-08-05 13:32:37 - ../code/strnpos.phps 1126 bytes - 2007-05-16 10:41:20 - ../code/recursive_readdir.phps 1126 bytes - 2007-05-16 10:41:20 - ../code/recursive_readdir.php 952 bytes - 2005-01-20 23:13:16 - ../code/hexstr.php 952 bytes - 2005-01-20 23:13:16 - ../code/hexstr.phps 950 bytes - 2005-01-15 22:53:12 - ../code/wordwrap_bigwords.php 950 bytes - 2005-01-15 22:53:12 - ../code/wordwrap_bigwords.phps 745 bytes - 2006-08-03 01:50:56 - ../code/str_split.phps 745 bytes - 2006-08-03 01:50:56 - ../code/str_split.php 681 bytes - 2008-05-06 20:09:31 - ../code/bcceil.php 676 bytes - 2008-01-11 16:49:50 - ../code/tail-f.phps 676 bytes - 2008-01-11 16:49:50 - ../code/tail-f.php 652 bytes - 2007-06-01 22:11:35 - ../code/str_rotN.php 600 bytes - 2007-05-16 11:48:40 - ../code/runtimer.phps 600 bytes - 2007-05-16 11:48:40 - ../code/runtimer.php 491 bytes - 2008-03-10 10:48:13 - ../code/ftruncate.php 481 bytes - 2007-06-01 22:29:03 - ../code/days_of_year.php 374 bytes - 2006-05-24 17:13:36 - ../code/number_to_words_ex.phps 374 bytes - 2006-05-24 17:13:36 - ../code/number_to_words_ex.php 324 bytes - 2007-04-25 12:34:52 - ../code/reentrant.php 294 bytes - 2006-06-26 15:52:38 - ../code/argparse.php 231 bytes - 2005-08-11 01:01:04 - ../code/single_astmatch.php 59 bytes - 2007-04-25 12:31:28 - ../code/reentrant_f2.php 59 bytes - 2007-04-25 12:31:33 - ../code/reentrant_f1.php 41 bytes - 2006-05-25 13:20:04 - ../code/foo.php 0 bytes - 2006-11-01 14:34:32 - ../code/testdir/dir2/dir2.2/testfile4 0 bytes - 2005-01-18 17:29:58 - ../code/200501152253 0 bytes - 2006-11-01 14:33:37 - ../code/testdir/dir3/testfile3 0 bytes - 2006-11-01 14:36:01 - ../code/testdir/dir2/dir2.2/subdir/1/2/3/deepfile 0 bytes - 2006-11-01 14:33:21 - ../code/testdir/dir2/testfile2 0 bytes - 2006-11-01 14:33:10 - ../code/testdir/testfile1