Bug #79365 [NEW]: preg_match_all should have a limit parameter
| From: | php4fan at gmail dot com | Date: | Tue, 10 Mar 2020 15:59:44 +0000 |
| Subject: | Bug #79365 [NEW]: preg_match_all should have a limit parameter | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-226012@lists.php.net to get a copy of this message | ||
From: php4fan at gmail dot com
Operating system: all
PHP version: 7.4.4RC1
Package: *General Issues
Bug Type: Bug
Bug description:preg_match_all should have a limit parameter
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 bug report at https://bugs.php.net/bug.php?id=79365&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=79365&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=79365&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=79365&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=79365&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=79365&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=79365&r=support
Expected behavior: https://bugs.php.net/fix.php?id=79365&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=79365&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=79365&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=79365&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=79365&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=79365&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=79365&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=79365&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=79365&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=79365&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=79365&r=mysqlcfg