Bug #81486 [NEW]: named capture group key missing if not found

From: Date: Wed, 29 Sep 2021 06:50:29 +0000
Subject: Bug #81486 [NEW]: named capture group key missing if not found
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-236906@lists.php.net to get a copy of this message
From: hkirsman at gmail dot com Operating system: Linux (Docker container) PHP version: 7.4.24 Package: *General Issues Bug Type: Bug Bug description:named capture group key missing if not found Description: ------------ My PHP version is actually 7.4.23. My issue is that I have regex named capture group (to catch language from URL) and sometimes the key is not there if nothing is found. It was working fine at some point as it was returning keys with empty strings. At some point keys were not there anymore. All I could think of is that I had made small changes to my regex. Is this a bug or working ok? I've added more comments in the script. Test script: --------------- <?php $path = '/elearning'; # Does not add empty lang to $matches. $regex_path_lang_match_not_found = '#/(?P<lang>[^/]+/)?elearning#'; # Adds empty lang to $matches. $regex_path_empty_lang_match_found = '#(?P<lang>)/elearning$#'; $regex_match = preg_match($regex_path_lang_match_not_found . 'u', $path, $matches); // Doc says If PREG_UNMATCHED_AS_NULL flag is passed, unmatched subpatterns are // reported as null; otherwise they are reported as an empty string. echo 'lang key is gone but should it?'; print_r($matches); $regex_match = preg_match($regex_path_lang_match_not_found . 'u', $path, $matches, PREG_UNMATCHED_AS_NULL); echo 'PREG_UNMATCHED_AS_NULL adds it back'; print_r($matches); $regex_match = preg_match($regex_path_empty_lang_match_found . 'u', $path, $matches); echo 'Similar regex, lang is not matched, but key exists'; print_r($matches); Expected result: ---------------- lang key is gone but should it?Array ( [0] => /elearning [lang] => [1] => ) PREG_UNMATCHED_AS_NULL adds it backArray ( [0] => /elearning [lang] => [1] => ) Similar regex, lang is not matched, but key existsArray ( [0] => /elearning [lang] => [1] => ) Actual result: -------------- lang key is gone but should it?Array ( [0] => /elearning ) PREG_UNMATCHED_AS_NULL adds it backArray ( [0] => /elearning [lang] => [1] => ) Similar regex, lang is not matched, but key existsArray ( [0] => /elearning [lang] => [1] => ) -- Edit bug report at https://bugs.php.net/bug.php?id=81486&edit=1 -- Fix committed: https://bugs.php.net/fix.php?id=81486&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=81486&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=81486&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=81486&r=needscript Try newer version: https://bugs.php.net/fix.php?id=81486&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=81486&r=support Expected behavior: https://bugs.php.net/fix.php?id=81486&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=81486&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=81486&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=81486&r=globals PHP version support discontinued: https://bugs.php.net/fix.php?id=81486&r=phptooold Daylight Savings: https://bugs.php.net/fix.php?id=81486&r=dst IIS Stability: https://bugs.php.net/fix.php?id=81486&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=81486&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=81486&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=81486&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=81486&r=mysqlcfg

« previous php.bugs (#236906) next »