From b9c9321f1016c59521c84af66832d806e32b8b4a Mon Sep 17 00:00:00 2001 From: Tomas Husak Date: Wed, 11 Aug 2021 13:32:41 +0200 Subject: [PATCH] Fix + test --- src/Peachpie.Library/pcre.cs | 3 ++- tests/pcre/preg_match_006.php | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 tests/pcre/preg_match_006.php diff --git a/src/Peachpie.Library/pcre.cs b/src/Peachpie.Library/pcre.cs index 18d26c721..f840bb65d 100644 --- a/src/Peachpie.Library/pcre.cs +++ b/src/Peachpie.Library/pcre.cs @@ -981,7 +981,8 @@ namespace Pchp.Library { var g = groups[i]; var value = (g.Success || !unmatchedAsNull) ? g.Value : null; - var item = NewArrayItem(value, g.Index, offsetCapture); + int index = g.Success ? g.Index : -1; + var item = NewArrayItem(value, index, offsetCapture); // All groups should be named. if (g.IsNamedGroup) diff --git a/tests/pcre/preg_match_006.php b/tests/pcre/preg_match_006.php new file mode 100644 index 000000000..79d525164 --- /dev/null +++ b/tests/pcre/preg_match_006.php @@ -0,0 +1,8 @@ +foo)?(bar)/', 'bar', $matches, PREG_OFFSET_CAPTURE); + +$is_fooname = isset( $matches['fooname'] ) && -1 !== $matches['fooname'][1]; + +echo "is_fooname:" . ($is_fooname ? "true" : "false") . "\n"; //is_fooname:false \ No newline at end of file