<?php

$txt= <<<TXT
post text
<!--quote-->quote1<!--quote-->sub-quote1<!--/quote--><!--/quote-->
post text
<!--quote-->quote2<!--/quote-->
post text
TXT;

$start=0; 

function step($txt,&$start,$tag=''){
    $format='<div class="%1$s">%2$s</div>';
    $result='';
    while(preg_match('/<!--(\/)?(\w+)-->/',$txt,$m,PREG_OFFSET_CAPTURE,$start)){
       if($start<$m[0][1]){
            $result.=substr($txt,$start,$m[0][1]-$start);
            $start=$m[0][1];
        }
        if($m[1][0]==''){
            $start+=strlen($m[0][0]);
            $result.=step($txt,$start,$m[2][0]);
        } else {
            $start+=strlen($m[0][0]);
            return sprintf($format,$tag,$result);
        }
    }
    return $result.substr($txt,$start,strlen($txt)-$start);
}

echo step($txt,$start);
