0
{Hello} {my} {name} {is} {Bob}

How can I loop in all the world in between the { and } ?.

$matches = array();
$t = preg_match_all('/{(.*?)\}/s', '{Hello} {my} {name} {is} {Bob}', $matches);
foreach ($matches as $match) {
    // Should display Hello, my, name, is, Bob
}
roberto
  • 1
  • 2
  • Also helpful: [loop over data extract all email addresses that ends with .co.uk](https://stackoverflow.com/a/32202946/2943403) and [Extracting numbers from string - Why do I get two arrays when using a capture group?](https://stackoverflow.com/q/41276719/2943403) and [Get specific data from txt file](https://stackoverflow.com/q/57198457/2943403) – mickmackusa Nov 03 '22 at 23:46

1 Answers1

0

The captured groups from preg_match_all() appear in an array $matches[1].

To get the result you want you can just implode() that array...

<?php

$matches = array();
$t = preg_match_all('/{(.*?)\}/s', '{Hello} {my} {name} {is} {Bob}', $matches);
echo implode(',',$matches[1]);   //Hello,my,name,is,Bob

See: https://3v4l.org/TINmH

If you want to loop over the results use

    foreach($matches[1] as $match) {
        // do stuff
    }