<?php $txt= <<<TXT post text <!--quote-->quote1<!--quote-->sub-quote1<!--/quote--><!--/quote--> post text <!--quote-->quote2<!--/quote--> post text TXT; $nested = 0; $set = []; foreach($result as $r) { if($r == '<!--quote-->') { $nested += 1;} elseif($r == '<!--/quote-->') { $nested -= 1; } else { $set[] = [$r,$nested]; } }
Standard input is empty
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, ), )