As of PHP 7.2, you can use the following.
If you work with named subpatterns and dont want to bother with unnamed match result entries and unmatched subpatterns, just replace preg_match() with named_preg_match(). This filters all unwanted stuff out.
<?php
function named_preg_match(string $pattern , string $subject, array &$matches = null, int $flags = 0, int $offset = 0) {
$retval = preg_match($pattern, $subject, $localmatches, PREG_UNMATCHED_AS_NULL | $flags, $offset);
if ($retval) {
foreach ($localmatches as $key => $value) {
if (is_int($key)) $value = null;
if (is_null($value)) unset($localmatches[$key]);
}
$matches = $localmatches;
}
return $retval;
}
?>
Hope this will be useful.