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 - PHP Tutorial
From the course: Learning the Standard PHP Library
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
(Locked)
Filtering a directory with GlobIterator5m 14s
-
(Locked)
Using regular expressions to filter directories with RegexIterator3m 49s
-
(Locked)
Extracting data from XML with RegexIterator and SimpleXML6m 25s
-
(Locked)
Specifying a range of items with LimitIterator4m 21s
-
(Locked)
Creating a custom filter with CallbackFilterIterator5m 6s
-
(Locked)
Creating a recursive callback filter5m 42s
-
(Locked)
Extending FilterIterator to create a reusable filter5m 10s
-
(Locked)
Creating a custom filter with RecursiveFilterIterator4m 48s
-
(Locked)
Overriding the RecursiveFilterIterator constructor5m 16s
-
(Locked)
Using ParentIterator to find elements that have children3m 59s
-
(Locked)
-
-
-
-
-