php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #79365 preg_match_all should have a limit parameter
Submitted: 2020-03-10 15:59 UTC Modified: 2020-03-11 08:28 UTC
From: php4fan at gmail dot com Assigned:
Status: Suspended Package: PCRE related
PHP Version: 7.4.4RC1 OS: all
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: php4fan at gmail dot com
New email:
PHP Version: OS:

 

 [2020-03-10 15:59 UTC] 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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-03-11 08:28 UTC] cmb@php.net
-Status: Open +Status: Suspended -Type: Bug +Type: Feature/Change Request -Package: *General Issues +Package: PCRE related
 [2020-03-11 08:28 UTC] cmb@php.net
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>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 09:01:27 2024 UTC