php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #67520 preg_match should capture always all parentheses
Submitted: 2014-06-26 07:03 UTC Modified: 2014-07-01 06:55 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: hejdav at centrum dot cz Assigned:
Status: Not a bug Package: *Regular Expressions
PHP Version: Irrelevant OS: Windows 7
Private report: No CVE-ID: None
 [2014-06-26 07:03 UTC] hejdav at centrum dot cz
Description:
------------
When preg_match() is called, I'm expecting that $matches will contain always one index per one capturing group. But when there is capturing group at the end of the pattern and it is not matched, $matches does not contain its index.


Test script:
---------------
$regexp = '/foo( bar)?/';
preg_match($regexp, 'foo', $matches);
print_r($matches);

Expected result:
----------------
Array (
  [0] => foo
  [1] =>
)

Actual result:
--------------
Array (
  [0] => foo
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-07-01 06:55 UTC] ab@php.net
-Status: Open +Status: Not a bug
 [2014-07-01 06:55 UTC] ab@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

preg_match_all() is probably what can fulfill your expectation.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Nov 30 03:03:16 2021 UTC