Simple way is whith every starting tag write to a output '\n' plus $counter times write '\t' and increment counter variable and for every ending tag decrement $counter and write '\n' plus $counter times write '\t'.
$counter = 0;
function start_tag_xxx( ... ) {
global $counter;
...
echo '\n'; $i = $counter++;
while ($i--) echo '\t';
...
}
function end_tag_xxx( ... ) {
global $counter;
...
echo '\n'; $i = --$counter;
while ($i--) echo '\t';
...
}
$counter is global