Bug #50887 [Wfx]: preg_match , last optional sub-patterns ignored when empy

From: Date: Thu, 21 Mar 2019 15:03:53 +0000
Subject: Bug #50887 [Wfx]: preg_match , last optional sub-patterns ignored when empy
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-220115@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=50887&edit=1 ID: 50887 Updated by: nikic@php.net Reported by: harrrrpo at gmail dot com Summary: preg_match , last optional sub-patterns ignored when empy Status: Wont fix Type: Bug Package: PCRE related Operating System: Windows PHP Version: 5.3.1 Block user comment: N Private report: N New Comment: @jflambert: The UNMATCHED_AS_NULL case will be fixed in PHP 7.4, see bug #73948. Previous Comments: ------------------------------------------------------------------------ [2019-03-21 14:56:10] jflambert at newtrax dot com I was about to file a duplicate defect when I found this one. Yes I agree, very aggravating that it's not at least in the documentation. For reference these were gonna be my EXPECTED RESULTS php > preg_match('/(a)(b)(c)*/', 'ab', $matches); php > var_dump($matches); php shell code:1: array(3) { [0] => string(2) "ab" [1] => string(1) "a" [2] => string(1) "b" [3] => string(0) "" } php > preg_match('/(a)(b)(c)*/', 'ab', $matches, PREG_UNMATCHED_AS_NULL); php > var_dump($matches); php shell code:1: array(3) { [0] => string(2) "ab" [1] => string(1) "a" [2] => string(1) "b" [3] => NULL } php > ------------------------------------------------------------------------ [2016-04-19 01:04:14] c639615 at trbvn dot com If you can't/won't fix this bug, PLEASE AT LEAST DOCUMENT IT IN THE MANUAL!!! ------------------------------------------------------------------------ [2011-08-03 12:37:58] cappuccino dot e dot cornetto at gmail dot com Delving into a fix for this bug, I found that it's not limited to last optional groups but to any last groups. In fact, the following: <code> $regex = '(?|(Sat)ur(day)|Sun(day)?)'; preg_match("@$regex@", 'Saturday', $matches); print_r($matches); preg_match("@$regex@", 'Sunday', $matches); print_r($matches); preg_match("@$regex@", 'Sun', $matches); print_r($matches); </code> prints: Array ( [0] => Saturday [1] => Sat [2] => day ) Array ( [0] => Sunday [1] => day ) Array ( [0] => Sun ) While it should print: Array ( [0] => Saturday [1] => Sat [2] => day ) Array ( [0] => Sunday [1] => day [2] => ) Array ( [0] => Sun [1] => [2] => ) ------------------------------------------------------------------------ [2011-07-13 08:35:20] c dot clix at tiscali dot it This is not a behaviour. This is a bug, causing occasional and unexpected errors with programs written based on the reference manual. Fixing the bug will avoid further occasional and unexpected errors. ------------------------------------------------------------------------ [2011-07-01 07:11:00] arveen dot ponniah at loginbn dot ch hello i'm tamilboy ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=50887 -- Edit this bug report at https://bugs.php.net/bug.php?id=50887&edit=1

« previous php.bugs (#220115) next »