Bug #73948 [NEW]: Preg_match_all should return empty strings on optional capture groups.

From: Date: Mon, 16 Jan 2017 14:24:56 +0000
Subject: Bug #73948 [NEW]: Preg_match_all should return empty strings on optional capture groups.
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-206673@lists.php.net to get a copy of this message
From:             tomasyorke at hotmail dot com
Operating system: Windows 7
PHP version:      7.0.14
Package:          *Regular Expressions
Bug Type:         Bug
Bug description:Preg_match_all should return empty strings on optional capture groups.

Description:
------------
Using preg_match_all with the PREG_SET_ORDER flag and an optional
capture group might return either an empty string or a missing element.


This depends on whether there is a matched capture group after the
non-matched capture group.

From an interface perspective, this means that I have to check for two
representations to test whether an optional capture group was matched.

The test script demonstrates such a case.

When considering a fix, the first priority should be that whatever the
representation is (NULL, an empty string or a missing element.),  it
should be consistent, no matter if there is a matched capture group
after or not.

If this cannot be fixed due to backwards compatibility Issues. Could we
add a PREG_KEEP_NONMATCHES or PREG_SET_ORDER_2 flag?


Test script:
---------------
<?php

preg_match_all("#(a)?(b)(c)?#","b",$matches,PREG_SET_ORDER);

var_dump($matches);

Expected result:
----------------
array(1) {
  [0] => array(3) {
    [0] => string(1) "b" [1] => string(0) "" [2] => string(1)
"b" [3] =>
string(0) ""
  }
}

Actual result:
--------------
array(1) {
  [0] => array(3) {
    [0] => string(1) "b" [1] => string(0) "" [2] => string(1)
"b"
  }
}

-- 
Edit bug report at https://bugs.php.net/bug.php?id=73948&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=73948&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=73948&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=73948&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=73948&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=73948&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=73948&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=73948&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=73948&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=73948&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=73948&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=73948&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=73948&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=73948&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=73948&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=73948&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=73948&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=73948&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=73948&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=73948&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=73948&r=mysqlcfg



Thread (19 messages)

« previous php.bugs (#206673) next »