fork(1) 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. $start=0;
  12.  
  13. function step($txt,&$start,$tag=''){
  14. $format='<div class="%1$s">%2$s</div>';
  15. $result='';
  16. while(preg_match('/<!--(\/)?(\w+)-->/',$txt,$m,PREG_OFFSET_CAPTURE,$start)){
  17. if($start<$m[0][1]){
  18. $result.=substr($txt,$start,$m[0][1]-$start);
  19. $start=$m[0][1];
  20. }
  21. if($m[1][0]==''){
  22. $start+=strlen($m[0][0]);
  23. $result.=step($txt,$start,$m[2][0]);
  24. } else {
  25. $start+=strlen($m[0][0]);
  26. return sprintf($format,$tag,$result);
  27. }
  28. }
  29. return $result.substr($txt,$start,strlen($txt)-$start);
  30. }
  31.  
  32. echo step($txt,$start);
  33.  
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
post text
<div class="quote">quote1<div class="quote">sub-quote1</div></div>
post text
<div class="quote">quote2</div>
post text