fork download
  1. <?php
  2.  
  3. $txt= <<<TXT
  4. post text
  5. <!--quote-->quote1<!--quote-->sub-quote1<!--/quote--><!--/quote-->
  6. post text
  7. <!--quote-->quote2<!--/quote-->
  8. post text
  9. TXT;
  10.  
  11. $result = preg_split('%(<!--.*?-->)%sim', $txt,-1,PREG_SPLIT_DELIM_CAPTURE);
  12.  
  13. $nested = 0;
  14. $set = [];
  15. foreach($result as $r) {
  16. if($r == '<!--quote-->') { $nested += 1;}
  17. elseif($r == '<!--/quote-->') { $nested -= 1; }
  18. else {
  19. $set[] = [$r,$nested];
  20. }
  21. }
  22.  
  23. var_export($set);
Success #stdin #stdout 0.03s 52480KB
stdin
Standard input is empty
stdout
array (
  0 => 
  array (
    0 => 'post text
',
    1 => 0,
  ),
  1 => 
  array (
    0 => 'quote1',
    1 => 1,
  ),
  2 => 
  array (
    0 => 'sub-quote1',
    1 => 2,
  ),
  3 => 
  array (
    0 => '',
    1 => 1,
  ),
  4 => 
  array (
    0 => '
post text
',
    1 => 0,
  ),
  5 => 
  array (
    0 => 'quote2',
    1 => 1,
  ),
  6 => 
  array (
    0 => '
post text',
    1 => 0,
  ),
)