You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This library gives you a way to ensure `preg_*` functions do not fail silently, returning
7
7
unexpected `null`s that may not be handled.
8
8
9
-
It also makes it easier to work with static analysis tools like PHPStan or Psalm as it
9
+
As of 2.0 this library also enforces [`PREG_UNMATCHED_AS_NULL`](#preg_unmatched_as_null) usage for all matching functions, [read more below](#preg_unmatched_as_null) to understand the implications.
10
+
11
+
It thus makes it easier to work with static analysis tools like PHPStan or Psalm as it
10
12
simplifies and reduces the possible return values from all the `preg_*` functions which
11
13
are quite packed with edge cases.
12
14
@@ -122,11 +124,39 @@ Due to type safety requirements a few restrictions are in place.
122
124
use `Preg::grep` in combination with some loop and `Preg::replace`.
123
125
-`replace`, `replaceCallback` and `replaceCallbackArray` do not support an array `$subject`,
124
126
only simple strings.
125
-
- in 2.x, we plan to always implicitly use `PREG_UNMATCHED_AS_NULL` for matching, which offers much
126
-
saner/predictable results. This is currently not doable due to the PHP version requirement and to
127
-
keep things working the same across all PHP versions. If you use the library on a PHP 7.2+ project
128
-
however we highly recommend using `PREG_UNMATCHED_AS_NULL` with all `match*` and `replaceCallback*`
129
-
methods.
127
+
- As of 2.0, the library always uses `PREG_UNMATCHED_AS_NULL` for matching, which offers [much
128
+
saner/more predictable results](#preg_unmatched_as_null). 3.x will also use the flag for
129
+
`replaceCallback` and `replaceCallbackArray`. This is currently not doable due to the PHP
130
+
version requirement and to keep things working the same across all PHP versions. If you use
131
+
the library on a PHP 7.4+ project however we highly recommend already passing
132
+
`PREG_UNMATCHED_AS_NULL` to `replaceCallback` and `replaceCallbackArray`.
133
+
134
+
#### PREG_UNMATCHED_AS_NULL
135
+
136
+
As of 2.0, this library always uses PREG_UNMATCHED_AS_NULL for all `match*` and `isMatch*`
137
+
functions (unfortunately `preg_replace_callback[_array]` only support this from PHP 7.4
138
+
onwards so we cannot do the same there yet).
139
+
140
+
This means your matches will always contain all matching groups, either as null if unmatched
141
+
or as string if it matched.
142
+
143
+
The advantages in clarity and predictability are clearer if you compare the two outputs of
144
+
running this with and without PREG_UNMATCHED_AS_NULL in $flags:
| group 2 (any unmatched group preceding one that matched) is set to `''`. You cannot tell if it matched an empty string or did not match at all | group 2 is `null` when unmatched and a string if it matched, easy to check for |
159
+
| group 4 (any optional group without a matching one following) is missing altogether. So you have to check with `isset()`, but really you want `isset($m[4]) && $m[4] !== ''` for safety unless you are very careful to check that a non-optional group follows it | group 4 is always set, and null in this case as there was no match, easy to check for with `$m[4] !== null`|
0 commit comments