note 95214 added to directoryiterator.construct

From: Date: Fri, 18 Dec 2009 20:55:14 +0000
Subject: note 95214 added to directoryiterator.construct
Groups: php.notes 
Request: Send a blank email to php-notes+get-164122@lists.php.net to get a copy of this message
Here's all-in-one DirectoryIterator: <?php /** * Real Recursive Directory Iterator */ class RRDI extends RecursiveIteratorIterator { /** * Creates Real Recursive Directory Iterator * @param string $path * @param int $flags * @return DirectoryIterator */ public function __construct($path, $flags = 0) { parent::__construct(new RecursiveDirectoryIterator($path, $flags)); } } /** * Real RecursiveDirectoryIterator Filtered Class * Returns only those items which filenames match given regex */ class AdvancedDirectoryIterator extends FilterIterator { /** * Regex storage * @var string */ private $regex; /** * Creates new AdvancedDirectoryIterator * @param string $path, prefix with '-R ' for recursive, postfix with /[wildcards] for matching * @param int $flags * @return DirectoryIterator */ public function __construct($path, $flags = 0) { if (strpos($path, '-R ') === 0) { $recursive = true; $path = substr($path, 3); } if (preg_match('~/?([^/]*\*[^/]*)$~', $path, $matches)) { // matched wildcards in filename $path = substr($path, 0, -strlen($matches[1]) - 1); // strip wildcards part from path $this->regex = '~^' . str_replace('*', '.*', str_replace('.', '\.', $matches[1])) . '$~'; // convert wildcards to regex if (!$path) $path = '.'; // if no path given, we assume CWD } parent::__construct($recursive ? new RRDI($path, $flags) : new DirectoryIterator($path)); } /** * Checks for regex in current filename, or matches all if no regex specified * @return bool */ public function accept() { // FilterIterator method return $this->regex === null ? true : preg_match($this->regex, $this->getInnerIterator()->getFilename()); } } ?> Some examples: <?php /* @var $i DirectoryIterator */ foreach (new AdvancedDirectoryIterator('.') as $i) echo $i->getPathname() . '<br/>'; // will output all files and directories in CWD foreach (new AdvancedDirectoryIterator('-R *.php') as $i) echo $i->getPathname() . '<br/>'; // will output all php files in CWD and all subdirectories foreach (new AdvancedDirectoryIterator('-R js/jquery-*.js') as $i) echo $i->getPathname() . '<br/>'; // will output all jQuery versions in directory js, or throw an exception if directory js doesn't exist ?> Pretty cool, huh? :) ---- Server IP: 82.177.164.100 Probable Submitter: 89.76.211.114 ---- Manual Page -- http://www.php.net/manual/en/directoryiterator.construct.php Edit -- https://master.php.net/note/edit/95214 Del: integrated -- https://master.php.net/note/delete/95214/integrated Del: useless -- https://master.php.net/note/delete/95214/useless Del: bad code -- https://master.php.net/note/delete/95214/bad+code Del: spam -- https://master.php.net/note/delete/95214/spam Del: non-english -- https://master.php.net/note/delete/95214/non-english Del: in docs -- https://master.php.net/note/delete/95214/in+docs Del: other reasons-- https://master.php.net/note/delete/95214 Reject -- https://master.php.net/note/reject/95214 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#164122) next »