php.net |  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
Votes:2
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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: php at sameprecision dot org
New email:
PHP Version: OS:

 

 [2010-05-21 16:54 UTC] php at sameprecision dot org
Description:
------------
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:
---------------
<?php

preg_match('%(a)|(b)%','b',$m);
var_dump($m);

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



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-06-07 16:52 UTC] cmb@php.net
-Status: Open +Status: Duplicate -Assigned To: +Assigned To: cmb
 [2016-06-07 16:52 UTC] cmb@php.net
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-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 13:01:29 2024 UTC