[php-src] Issue #15377: preg_match_callback()
| From: | nevez | Date: | Tue, 13 Aug 2024 11:36:58 +0000 |
| Subject: | [php-src] Issue #15377: preg_match_callback() | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-247487@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/15377
Author: nevez
### Description
I think it would be helpful when extracting sections or generally processing very large strings to
have a preg_match_callback() function.
Currently I use preg_replace_callback(), discarding the return value, but this approach probably has
a non-trivial overhead due to string copying and concatenation. It also feels a bit
"hacky".
```
preg_replace_callback($regex, function ($matches) use (&$selected) {
$selected .= $matches[1];
return '';
}, $block);
```
An hypothetical preg_match_callback() could return void or even better the number of matches, as
with preg_match_all():
```
$nSelected = preg_match_callback($regex, function ($matches) use (&$selected) {
$selected .= $matches[1];
}, $block);
```