note 81396 deleted from ref.dir by nlopess
| From: | nlopess@php.net | Date: | Tue, 26 Feb 2008 19:56:27 +0000 |
| Subject: | note 81396 deleted from ref.dir by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-136511@lists.php.net to get a copy of this message | ||
Note Submitter: eeccho
----
// Function for searching files recursively using a regex.
// $type - array of file types, which will be returned as a result.
// $realpath - if set to true, returns absolute paths instead of relative.
// Warning: regex is applied to the whole path (not only to file name).
<?php
function find_regex($path, $regex, $realpath = false, $type = array('file')) {
  $result = array();
  $realpath && $path = realpath($path);
  if (preg_match($regex, $path) && in_array(@filetype($path), $type)) {
    $result[] = $path;
  }
  if ($dh = @opendir($path)) {
    while (($obj = readdir($dh)) !== false) {
      if ($obj == '.' || $obj == '..') continue;
      $obj = $path . DIRECTORY_SEPARATOR . $obj;
      $ftype = @filetype($obj);
      if ($ftype !== 'dir') {
        in_array($ftype, $type) && preg_match($regex, $obj)
&& $result[] = $obj;
      } else {
        $result = array_merge($result, find_regex(&$obj,
&$regex, &$realpath, &$type));
      }
    }
  }
  return $result;
}