From the course: Learning the Standard PHP Library

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Using regular expressions to filter directories with RegexIterator

Using regular expressions to filter directories with RegexIterator - PHP Tutorial

From the course: Learning the Standard PHP Library

Start my 1-month free trial

Using regular expressions to filter directories with RegexIterator

- In the previous video, GlobIterator wasn't able to select both JPEG's and PNG files in a single operation. Moreover, we were limited to a single directory. Using a regular expression with RegexIterator, gives you more flexibility. This is regex.php, which you can find in the Chapter Three folder of the exercise files. It contains an instance of RecursiveDirectoryIterator, which is chained with RecursiveIteratorIterator. And the path that's passed as the value to RecursiveDirectoryIterator is a single dot, which means it'll start at the current directory. Since I've copied this file to the top level of my testing site, the foreach loop would list every file in the site. To filter the results, we need to create an instance of RegexIterator and chain it. So let's add a new line on line four. We'll keep the same name for the Iterator: "files," and it'll be "new," "RegexIterator." And the first argument is the Iterator that we want to filter, which is "files." The second argument needs…

Contents