Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Joshua Thijssen:
SPL Deepdive: RegexIterator
Feb 13, 2014 @ 17:18:55

Joshua Thijssen has posted a deep dive with the RegexIterator to his site today. This iterator is part of the SPL (Standard PHP Library) that is included with PHP. It allows you to filter items based a given regular expression.

If everything goes according to plan (which never is the case), I’ll try and highlight some of the fascinating stuff that can be found inside the SPL. I do a lot of presentations about the SPL, and one of the things I like to tell people is that even though the SPL, – iterators particularly – is a magnificent piece of code that is often underused and misunderstood, it does come with some quirks and glitches that aren’t documented properly. Today, i’ll explain a bit indepth the RegexIterator. This iterator extends the FilterIterator, meaning it can be used to filter out unwanted entries from parent iterators.

He gives a brief example of it in use to filter the results from a DirectoryIterator but quickly gets into the more advanced functionality. He looks at the different "modes" it can operate in: all matches, get matches, match, replace and split. He includes code examples and explanations for each of them, including the resulting output. He also shows how to filter based on the keys instead of values and briefly mentions the "preg_flags" but doesn't get too far into them as they change based on the mode being used.

tagged: spl standardphplibrary regexiterator deepdive tutorial

Link: https://www.adayinthelifeof.nl/2014/02/12/spl-deepdive-regexiterator


Trending Topics: