From the course: Learning the Standard PHP Library
Unlock the full course today
Join today to access over 22,700 courses taught by industry experts or purchase this course individually.
Creating a custom filter with CallbackFilterIterator - PHP Tutorial
From the course: Learning the Standard PHP Library
Creating a custom filter with CallbackFilterIterator
- The CallbackFilterIterator class allows you to create a custom filter using a callback function. It requires a minimum of PHP 5.4. If you're using PHP 5.3, the alternative is to extend the FilterIterator class which is described later in this chapter. I've got open callback_filter.php. It contains the simplexml that was used earlier in this chapter to extract data from courses.xml in the common/data directory. The only difference is that instead of getting the duration, this time we're getting the course level. The first thing we need to do is to convert the simplexml element into a simplexml iterator. We do that by adding a second argument to the simplexml_load_file. So that's on line two and it's a string SimpleXMLIterator. Forgetting to convert simplexml elements into iterators when you want to use them with other iterators is a common problem, so don't forget to do that. So, now we can chain this with the CallbackFilterIterator. I think I'm going to need quite a lot of space so…
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)
-
-
-
-
-