consider following code:
preg_match('/(\d)(\d?)/', '1', $matches); var_export($matches); it outputs:
array ( 0 => '1', 1 => '1', 2 => '', ) so optional [2] entry set, empty string.
however, remember in past have seen cases optional capturing group, when used last, not set (the array stop @ [1]).
did dream, or there cases entry can not set?
i know use if (! isset($matches[2]) || $matches[2] == '') if want sure, clutter code needlessly.
it depends on whether make whole capturing group optional or not.
if make group required allow match empty string match set, might empty:
preg_match('/(\d)(\d?)/', '1', $matches); // $matches[2] === '' if make group optional moving quantifier ? outside brackets, match set conditionally:
preg_match('/(\d)(\d)?/', '1', $matches); // no $matches[2]
Comments
Post a Comment