Bug->Req #79365 [Opn->Sus]: preg_match_all should have a limit parameter
| From: | cmb@php.net | Date: | Wed, 11 Mar 2020 08:28:17 +0000 |
| Subject: | Bug->Req #79365 [Opn->Sus]: preg_match_all should have a limit parameter | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-226019@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79365&edit=1
ID: 79365
Updated by: cmb@php.net
Reported by: php4fan at gmail dot com
Summary: preg_match_all should have a limit parameter
-Status: Open
+Status: Suspended
-Type: Bug
+Type: Feature/Change Request
-Package: *General Issues
+Package: PCRE related
Operating System: all
PHP Version: 7.4.4RC1
Block user comment: N
Private report: N
New Comment:
Well, this is certainly not a bug, but rather a limitation, if at
all. Some questions that come to mind: is the given use case
sufficiently common to warrant it covered by a core function? Do
we really want to add a 6th (!) parameter to preg_match_all(), or
maybe introduce a distinct funtion for this purpose? Or maybe we
should go a step further, and add new APIs for PCRE?
I expect the answers to these (and maybe more) questions to
require quite some discussion, for which this bug tracker is not
suitable. Instead, please bring up this feature request on the
internal mailing list[1]. For the time being, I'm suspending this
ticket.
[1] <https://www.php.net/mailing-lists.php#internals>
Previous Comments:
------------------------------------------------------------------------
[2020-03-10 15:59:44] php4fan at gmail dot com
Description:
------------
All preg matching functions except preg_match_all (e.g. preg_split, preg_replace,
preg_replace_callback, etc.) accept a $limit parameter that allows you to specify the maximum number
of occurrences to search for, after which the search is stopped.
There's no good reason why preg_match_all shouldn't accept a similar parameter.
Say I have a very long string that I expect to have hundreds of thousands of occurrences of a given
pattern but I want to find only the first N with N a very small number. The only option I have is to
use preg_match_all to find all the occurrences and then only use the first N: a huge waste of
resources. In order to avoid that I would have to use preg_replace_callback with a limit and with a
callback that returns the string unchanged (because I don't want to replace, just to find) and
stores it somewhere for later use. Or use preg_match() N times with the PREG_SPLIT_OFFSET_CAPTURE
option and the $offset parameter. All ridiculously convoluted workarounds to something that should
be straight-forward.
For BC, this should be added as a 6th parameter after $offset.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79365&edit=1