I have the following string:
<?foo?> <?bar?> <?baz?> hello world <?/?> <?/?> <?/?>
I need a regular expression to convert it into
<?foo?> <?bar?> <?baz?> hello world <?/baz?> <?/bar?> <?/foo?>
The following code works for non-recursive tags:
$x=preg_replace_callback('/.*?<\?\/\?>/',function($x){
return preg_replace('/(.*<\?([^\/][\w]+)\?>)(.*?)(<\?\/?\?>)/s',
'\1\3<?/\2?>',$x[0]);
},$str);