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/morse.php
../code/wordwrap_bigwords.phps
../code/array_unique_recursive.php
../code/hexstr.php
../code/str_split.php
../code/validate_email.inc.phps.bak
../code/parse_str_quoted.inc.php
../code/argparse.php
../code/foo.php
../code/str_case.phps
../code/404.phps
../code/recursive_readdir.php
../code/recursive_readdir.phps
../code/tail-10.phps
../code/single_astmatch.php
../code/maillog.phps
../code/wordwrap_bigwords.php
../code/morse.phps
../code/number_to_words_ex.php
../code/ftruncate.php
../code/str_case.php
../code/showvalidemail.php
../code/code_debug_timing.php
../code/validate_email.inc.phps
../code/200501152253
../code/number_to_words_apathy.php
../code/rc4.inc.phps
../code/days_of_year.php
../code/dirlister.php
../code/tail-f.php
../code/dominicsayers.php
../code/runtimer.php
../code/str_split.phps
../code/number_to_words_ex.phps
../code/number_to_words.phps
../code/number_to_words.php
../code/404.php
../code/tail-10.php
../code/bcceil.php
../code/showvalidemail.php.txt
../code/strnpos.phps
../code/runtimer.phps
../code/tail-f.phps
../code/array_unique_recursive.phps
../code/hexstr.phps
../code/strnpos.php
../code/str_rotN.php
../code/dirlister.phps

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/morse.php
../code/wordwrap_bigwords.phps
../code/array_unique_recursive.php
../code/hexstr.php
../code/testdir/testfile1
../code/testdir/dir2/dir2.2/subdir/1/2/3/deepfile
../code/testdir/dir2/dir2.2/testfile4
../code/testdir/dir2/testfile2
../code/testdir/dir3/testfile3
../code/str_split.php
../code/validate_email.inc.phps.bak
../code/parse_str_quoted.inc.php
../code/argparse.php
../code/foo.php
../code/str_case.phps
../code/404.phps
../code/recursive_readdir.php
../code/recursive_readdir.phps
../code/tail-10.phps
../code/single_astmatch.php
../code/maillog.phps
../code/wordwrap_bigwords.php
../code/morse.phps
../code/number_to_words_ex.php
../code/ftruncate.php
../code/str_case.php
../code/showvalidemail.php
../code/code_debug_timing.php
../code/validate_email.inc.phps
../code/200501152253
../code/number_to_words_apathy.php
../code/rc4.inc.phps
../code/days_of_year.php
../code/dirlister.php
../code/tail-f.php
../code/dominicsayers.php
../code/runtimer.php
../code/str_split.phps
../code/number_to_words_ex.phps
../code/number_to_words.phps
../code/number_to_words.php
../code/404.php
../code/tail-10.php
../code/bcceil.php
../code/showvalidemail.php.txt
../code/strnpos.phps
../code/runtimer.phps
../code/tail-f.phps
../code/array_unique_recursive.phps
../code/hexstr.phps
../code/strnpos.php
../code/str_rotN.php
../code/dirlister.phps

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_split.php
../code/str_case.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_split.php] => Array
        (
            [stat] => Array
                (
                    [0] => 2051
                    [1] => 68944760
                    [2] => 33188
                    [3] => 1
                    [4] => 1000
                    [5] => 1000
                    [6] => 0
                    [7] => 745
                    [8] => 1511162410
                    [9] => 1154566256
                    [10] => 1413386008
                    [11] => 4096
                    [12] => 8
                    [dev] => 2051
                    [ino] => 68944760
                    [mode] => 33188
                    [nlink] => 1
                    [uid] => 1000
                    [gid] => 1000
                    [rdev] => 0
                    [size] => 745
                    [atime] => 1511162410
                    [mtime] => 1154566256
                    [ctime] => 1413386008
                    [blksize] => 4096
                    [blocks] => 8
                )

            [du] => 4096
            [uid] => 1000
            [gid] => 1000
            [filetype] => file
            [mimetype] => text/x-php
            [dirname] => ../code
            [basename] => str_split.php
            [ext] => php
            [owner] => Array
                (
                    [name] => pgregg
                    [passwd] => x
                    [uid] => 1000
                    [gid] => 1000
                    [gecos] => 
                    [dir] => /home/pgregg
                    [shell] => /bin/bash
                )

        )

    [../code/str_case.php] => Array
        (
            [stat] => Array
                (
                    [0] => 2051
                    [1] => 68944758
                    [2] => 33188
                    [3] => 1
                    [4] => 1000
                    [5] => 1000
                    [6] => 0
                    [7] => 2903
                    [8] => 1511198380
                    [9] => 1124413021
                    [10] => 1413386008
                    [11] => 4096
                    [12] => 8
                    [dev] => 2051
                    [ino] => 68944758
                    [mode] => 33188
                    [nlink] => 1
                    [uid] => 1000
                    [gid] => 1000
                    [rdev] => 0
                    [size] => 2903
                    [atime] => 1511198380
                    [mtime] => 1124413021
                    [ctime] => 1413386008
                    [blksize] => 4096
                    [blocks] => 8
                )

            [du] => 4096
            [uid] => 1000
            [gid] => 1000
            [filetype] => file
            [mimetype] => text/x-php
            [dirname] => ../code
            [basename] => str_case.php
            [ext] => php
            [owner] => Array
                (
                    [name] => pgregg
                    [passwd] => x
                    [uid] => 1000
                    [gid] => 1000
                    [gecos] => 
                    [dir] => /home/pgregg
                    [shell] => /bin/bash
                )

        )

    [../code/str_rotN.php] => Array
        (
            [stat] => Array
                (
                    [0] => 2051
                    [1] => 68944759
                    [2] => 33188
                    [3] => 1
                    [4] => 1000
                    [5] => 1000
                    [6] => 0
                    [7] => 652
                    [8] => 1511198927
                    [9] => 1180732295
                    [10] => 1413386008
                    [11] => 4096
                    [12] => 8
                    [dev] => 2051
                    [ino] => 68944759
                    [mode] => 33188
                    [nlink] => 1
                    [uid] => 1000
                    [gid] => 1000
                    [rdev] => 0
                    [size] => 652
                    [atime] => 1511198927
                    [mtime] => 1180732295
                    [ctime] => 1413386008
                    [blksize] => 4096
                    [blocks] => 8
                )

            [du] => 4096
            [uid] => 1000
            [gid] => 1000
            [filetype] => file
            [mimetype] => text/x-php
            [dirname] => ../code
            [basename] => str_rotN.php
            [ext] => php
            [owner] => Array
                (
                    [name] => pgregg
                    [passwd] => x
                    [uid] => 1000
                    [gid] => 1000
                    [gecos] => 
                    [dir] => /home/pgregg
                    [shell] => /bin/bash
                )

        )

)

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/array_unique_recursive.php
../code/array_unique_recursive.phps
../code/bcceil.php
../code/code_debug_timing.php
../code/days_of_year.php
../code/dirlister.php
../code/dirlister.phps
../code/dominicsayers.php
../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/parse_str_quoted.inc.php
../code/rc4.inc.phps
../code/recursive_readdir.php
../code/recursive_readdir.phps
../code/runtimer.php
../code/runtimer.phps
../code/showvalidemail.php
../code/showvalidemail.php.txt
../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/validate_email.inc.phps.bak
../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.bak
../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.txt
../code/showvalidemail.php
../code/runtimer.phps
../code/runtimer.php
../code/recursive_readdir.phps
../code/recursive_readdir.php
../code/rc4.inc.phps
../code/parse_str_quoted.inc.php
../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/dominicsayers.php
../code/dirlister.phps
../code/dirlister.php
../code/days_of_year.php
../code/code_debug_timing.php
../code/bcceil.php
../code/array_unique_recursive.phps
../code/array_unique_recursive.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) 29911 ../code/showvalidemail.php.txt
2) 29911 ../code/showvalidemail.php
3) 13584 ../code/dominicsayers.php
4) 5987 ../code/dirlister.phps
5) 5987 ../code/dirlister.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) 2010-11-29 17:02:30 - 2606 bytes - ../code/parse_str_quoted.inc.php
2) 2010-03-31 14:24:56 - 3184 bytes - ../code/404.php
3) 2010-03-31 14:24:56 - 3184 bytes - ../code/404.phps
4) 2009-06-28 19:36:04 - 29911 bytes - ../code/showvalidemail.php.txt
5) 2009-06-28 19:36:04 - 29911 bytes - ../code/showvalidemail.php
6) 2009-04-23 21:34:30 - 3155 bytes - ../code/tail-10.php
7) 2009-04-23 21:34:30 - 3155 bytes - ../code/tail-10.phps
8) 2009-02-25 17:36:59 - 3170 bytes - ../code/validate_email.inc.phps
9) 2009-02-25 16:46:34 - 13584 bytes - ../code/dominicsayers.php
10) 2009-02-21 14:59:11 - 5987 bytes - ../code/dirlister.phps




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


Files, recursive, in date modified order:
1379285917 - 2013-09-15 22:58:37 - ./cisco.txt 1302473128 - 2011-04-10 22:05:28 - ./old/2.0/preg_find.phps 1302473128 - 2011-04-10 22:05:28 - ./old/2.0/preg_find.php 1244846831 - 2009-06-12 22:47:11 - ./old/2.3/preg_find.php 1244846827 - 2009-06-12 22:47:07 - ./preg_find.phps 1244846827 - 2009-06-12 22:47:07 - ./preg_find.php.txt 1244846827 - 2009-06-12 22:47:07 - ./preg_find.php 1244846815 - 2009-06-12 22:46:55 - ./old/2.2/preg_find.php 1244846584 - 2009-06-12 22:43:04 - ./preg_find_ex.phps 1244846584 - 2009-06-12 22:43:04 - ./preg_find_ex.php 1216163127 - 2008-07-15 23:05:27 - ./old/2.1/preg_find.php 1176804073 - 2007-04-17 10:01:13 - ./old/2.0/preg_find_ex.phps 1176804073 - 2007-04-17 10:01:13 - ./old/2.0/preg_find_ex.php


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


Files in ../code, recursive, in reverse file size order:
29911 bytes - 2009-06-28 19:36:04 - ../code/showvalidemail.php.txt 29911 bytes - 2009-06-28 19:36:04 - ../code/showvalidemail.php 13584 bytes - 2009-02-25 16:46:34 - ../code/dominicsayers.php 5987 bytes - 2009-02-21 14:59:11 - ../code/dirlister.phps 5987 bytes - 2009-02-21 14:59:11 - ../code/dirlister.php 3184 bytes - 2010-03-31 14:24:56 - ../code/404.php 3184 bytes - 2010-03-31 14:24:56 - ../code/404.phps 3170 bytes - 2009-02-25 17:36:59 - ../code/validate_email.inc.phps 3155 bytes - 2009-04-23 21:34:30 - ../code/tail-10.phps 3155 bytes - 2009-04-23 21:34:30 - ../code/tail-10.php 2997 bytes - 2006-05-24 14:22:31 - ../code/number_to_words.php 2997 bytes - 2006-05-24 14:22:31 - ../code/number_to_words.phps 2903 bytes - 2005-08-19 00:57:01 - ../code/str_case.php 2903 bytes - 2005-08-19 00:57:01 - ../code/str_case.phps 2606 bytes - 2010-11-29 17:02:30 - ../code/parse_str_quoted.inc.php 2246 bytes - 2008-08-10 19:34:44 - ../code/array_unique_recursive.php 2246 bytes - 2008-08-10 19:34:44 - ../code/array_unique_recursive.phps 2013 bytes - 2007-10-01 22:58:02 - ../code/code_debug_timing.php 2012 bytes - 2006-05-24 14:33:30 - ../code/number_to_words_apathy.php 1950 bytes - 2007-06-01 22:03:52 - ../code/rc4.inc.phps 1827 bytes - 2008-05-29 00:59:40 - ../code/validate_email.inc.phps.bak 1497 bytes - 2007-06-08 10:50:50 - ../code/morse.php 1497 bytes - 2007-06-08 10:50:50 - ../code/morse.phps 1467 bytes - 2005-08-05 13:10:19 - ../code/maillog.phps 1322 bytes - 2009-02-19 19:00:43 - ../code/hexstr.php 1322 bytes - 2009-02-19 19:00:43 - ../code/hexstr.phps 1260 bytes - 2005-08-05 12:32:37 - ../code/strnpos.phps 1260 bytes - 2005-08-05 12:32:37 - ../code/strnpos.php 1126 bytes - 2007-05-16 09:41:20 - ../code/recursive_readdir.phps 1126 bytes - 2007-05-16 09:41:20 - ../code/recursive_readdir.php 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 00:50:56 - ../code/str_split.phps 745 bytes - 2006-08-03 00:50:56 - ../code/str_split.php 681 bytes - 2008-05-06 19: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 21:11:35 - ../code/str_rotN.php 600 bytes - 2007-05-16 10:48:40 - ../code/runtimer.phps 600 bytes - 2007-05-16 10:48:40 - ../code/runtimer.php 491 bytes - 2008-03-10 10:48:13 - ../code/ftruncate.php 481 bytes - 2007-06-01 21:29:03 - ../code/days_of_year.php 374 bytes - 2006-05-24 16:13:36 - ../code/number_to_words_ex.php 374 bytes - 2006-05-24 16:13:36 - ../code/number_to_words_ex.phps 294 bytes - 2006-06-26 14:52:38 - ../code/argparse.php 231 bytes - 2005-08-11 00:01:04 - ../code/single_astmatch.php 41 bytes - 2006-05-25 12:20:04 - ../code/foo.php 0 bytes - 2006-11-01 14:33:37 - ../code/testdir/dir3/testfile3 0 bytes - 2006-11-01 14:33:21 - ../code/testdir/dir2/testfile2 0 bytes - 2006-11-01 14:33:10 - ../code/testdir/testfile1 0 bytes - 2005-01-18 17:29:58 - ../code/200501152253 0 bytes - 2006-11-01 14:36:01 - ../code/testdir/dir2/dir2.2/subdir/1/2/3/deepfile 0 bytes - 2006-11-01 14:34:32 - ../code/testdir/dir2/dir2.2/testfile4