|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #51881 preg_match: Capturing subpatterns that don't match get empty string.
Submitted: 2010-05-21 16:54 UTC Modified: 2016-06-07 16:52 UTC
Avg. Score:4.0 ± 1.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: php at sameprecision dot org Assigned: cmb (profile)
Status: Duplicate Package: PCRE related
PHP Version: 5.3.2 OS: Win XP Pro
Private report: No CVE-ID: None
 [2010-05-21 16:54 UTC] php at sameprecision dot org
Capturing subpatterns that don't match anything still show an empty string in the match array.

Since empty string is a legitimate value, the match array should have null or false instead.

The workaround to detect a capturing subpattern not matching anything is to use PREG_OFFSET_CAPTURE and look for offset -1 for the particular subpattern.

Test script:


//$m[1] === "" as if the first capturing parentheses matched ""
//$m[1] === null would be more appropriate (javascript uses "undefined")


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-06-07 16:52 UTC]
-Status: Open +Status: Duplicate -Assigned To: +Assigned To: cmb
 [2016-06-07 16:52 UTC]
Actually, bug #61780 is a duplicate of this report, but as the
other ticket has already a PR attached, I'm closing this one.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Wed Mar 29 04:03:53 2023 UTC