There are several PCRE functions available, but today I looked for one that just wasn’t there:
Could’ve maybe used
preg_replace_callback(), but felt wrong since I didn’t actually want to do any replacing. I just needed my function to be called for each match.
So I wrote it myself. Noting it here, in case I (or someone else) needs it again.
* Perform a global regular expression match
* and calls the callback for each match.
$r = preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);
foreach($matches ??  as $match)
And, in case someone reads this post and knows it actually does exist… and if that someone is you, please do leave a comment!
And, yes, I could’ve just written those 3 lines where I needed them, but what’s the fun in that? And besides, the shorter the code where it counts, the easier what counts is to read.
0 => string 'Hello' (length=5)
1 => string 'H' (length=1)
0 => string 'World' (length=5)
1 => string 'W' (length=1)