Understand PREG_UNMATCHED_AS_NULL behavior
mainThe library enforces PREG_UNMATCHED_AS_NULL for all matching and replacement callback functions.
Implications:
- All matching groups will always be present in the
$matchesarray. - If a group did not match, its value will be
nullrather than the group being missing from the array or being an empty string. - This makes it easy to distinguish between a group that matched an empty string and a group that did not match at all.
Example Comparison:
For the pattern /(a)(b)*(c)(d)*/ against the string 'ac':
| Feature | Without Flag | With PREG_UNMATCHED_AS_NULL |
|---|---|---|
| Array Size | 4 | 5 |
| Group 2 (unmatched) | '' (cannot tell if matched empty or not matched) | null (explicitly unmatched) |
| Group 4 (unmatched) | Missing (requires isset()) | null (always present, easy to check with $m[4] !== null) |