Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

untymage's avatar

Can i do a preg_match_all with collection ?

This my vanilla php:

preg_match_all('~my regex pattern that capture 5 groups ~', $content, $m);

array_map(fn($l, $s, $e, $b) => [
    'line' => $l,
    'start' => $s,
    'end' => $e,
    'body' => $b
], $m[2], $m[3], $m[4], $m[5]);

Is it possible to do this in laravel collection ? I tried Str::of($content)->matchAll() but the problem is, it only return group 1 or 0 but my regex capture 5 group, even i opened an issue in github https://github.com/laravel/framework/issues/35509 he ignore without reading my issue

0 likes
2 replies
tisuchi's avatar

@untymage If it is the collection, what if you use get()->each(), then pass the regular expression inside the closure in each() method?

untymage's avatar

$content is string, the $m variable contains 5 groups of arrays

Please or to participate in this conversation.