fork download
  1. <?php
  2. $s = <<<INPUT
  3. [startA]
  4. this is the first group
  5.  [startB]
  6.  blabla
  7. [end]
  8. [end]
  9. [startA]
  10. this is the second group
  11.  [startB]
  12.  blabla
  13. [end]
  14. [end]
  15. [startA]
  16. this is the second group
  17.  [startB]
  18.  blabla
  19. [end]
  20. [end]
  21. INPUT;
  22. preg_match_all('/\[(?!end)[^[\]]+\](?:[^[\]]*|[^[\]]*(?R)[^[\]]*)\[end\]\s*/', $s, $m);
  23. foreach ($m[0] as $match) print "$match\n";
  24. ?>
Success #stdin #stdout 0.02s 13112KB
stdin
Standard input is empty
stdout
[startA]
this is the first group
 [startB]
 blabla
[end]
[end]

[startA]
this is the second group
 [startB]
 blabla
[end]
[end]

[startA]
this is the second group
 [startB]
 blabla
[end]
[end]