1--TEST-- 2preg_replace_callback() 3--FILE-- 4<?php 5$input = "plain [indent] deep [indent] [abcd]deeper[/abcd] [/indent] deep [/indent] plain"; 6 7function parseTagsRecursive($input) 8{ 9 10 $regex = '#\[indent]((?:[^[]|\[(?!/?indent])|(?R))+)\[/indent]#'; 11 12 if (is_array($input)) { 13 $input = '<div style="margin-left: 10px">'.$input[1].'</div>'; 14 } 15 16 return preg_replace_callback($regex, 'parseTagsRecursive', $input); 17} 18 19$output = parseTagsRecursive($input); 20 21echo $output, "\n"; 22 23?> 24--EXPECT-- 25plain <div style="margin-left: 10px"> deep <div style="margin-left: 10px"> [abcd]deeper[/abcd] </div> deep </div> plain 26