Bug #81486 [Nab]: named capture group key missing if not found if not using PREG_UNMATCHED_AS_NULL
| From: | hkirsman at gmail dot com | Date: | Wed, 29 Sep 2021 09:05:39 +0000 |
| Subject: | Bug #81486 [Nab]: named capture group key missing if not found if not using PREG_UNMATCHED_AS_NULL | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-236909@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=81486&edit=1
ID: 81486
User updated by: hkirsman at gmail dot com
Reported by: hkirsman at gmail dot com
Summary: named capture group key missing if not found if not
using PREG_UNMATCHED_AS_NULL
Status: Not a bug
Type: Bug
Package: PCRE related
Operating System: Linux (Docker container)
PHP Version: 7.4.24
Block user comment: N
Private report: N
New Comment:
I've currently solved it with PREG_UNMATCHED_AS_NULL and seems to work fine.
Previous Comments:
------------------------------------------------------------------------
[2021-09-29 07:22:27] requinix@php.net
Without using PREG_UNMATCHED_AS_NULL, an unmatched group may or may not report as an empty string
depending on circumstances that aren't entirely obvious.
https://3v4l.org/MjB11
PREG_UNMATCHED_AS_NULL was added to PHP specifically to normalize this behavior and ensure that an
unmatched group is always added to the match array, and with a null value to distinguish it from a
match of a zero-length string.
https://3v4l.org/4H7GJ
The answer to your problem is probably either PREG_UNMATCHED_AS_NULL or simply ??
------------------------------------------------------------------------
[2021-09-29 06:50:29] hkirsman at gmail dot com
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 this bug report at https://bugs.php.net/bug.php?id=81486&edit=1