In your question, you don’t see which “match structure” you want ... But you can only use simple arrays. Try
preg_match_all('#\[([az\)\(]+?)\]#',$original,$m);
that for $original = 'bar[foo[test[abc][def]]bar]foo'
returns an array with "abc" and "def", internal.
For your output, you need a loop for the “parsing task”. PCRE with preg_replace_callback is better for parsing.
Perhaps this cycle is a good key to your problem,
$original = 'bar[foo[test[abc][def]]bar]foo'; for( $aux=$oldAux=$original; $oldAux!=($aux=printInnerBracket($aux)); $oldAux=$aux ); print "\n-- $aux"; function printInnerBracket($s) { return preg_replace_callback( '#\[([az\)\(]+?)\]#', // the only one regular expression function($m) { print "\n$m[0]"; return "($m[1])"; }, $s ); }
Result (callback):
[abc] [def] [test(abc)(def)] [foo(test(abc)(def))bar] -- bar(foo(test(abc)(def))bar)foo
See also this related question .
source share