Hello.

I read the documentation, I tried to write a regular list on its material. Something is not really moved forward (or rather not as desired). In general, I decided to write a regular book that will draw the text between the brackets. Here's what happened:

preg_match_all('/\[([A-Za-z0-9]+)\]/', '[text0[text01][text02[text023[text0234]]]][text1]', $m); // выводится: Array ( [0] => text01 [1] => text0234 [2] => text1 ) 

I try to pull out all the values ​​that lie between all the brackets. And write the result like this:

 Array( 0 => Array( 'text0' => Array( 0 => 'text01' ), Array( 'text02' => Array( 'text023' => Array( 0 => 'text0234' ) ) ) ), 1 => Array( 0 => 'text1' ) ); 

Those. each level of parentheses will be responsible for the new level of the array.

  • one
    Did you manage to find the answer? - L. Vadim
  • It didn’t work out, I decided to study better now for the regular season. Then I can solve this problem. - dgd hsk
  • I have the answer, without a multilevel array, as you need it, it only finds words, and the array of them - L. Vadim

1 answer 1

First you can find the top level of nesting:

 preg_match_all("/\[(((?>[^\[\]]+)|(?R))*)\]/", "[text0[text01][text02[text023[text0234]]]][text1]", $output_array); // $output_array[1]=array( //0 => text0[text01][text02[text023[text0234]]] //1 => text1 //) 

$output_array[1] will contain first level nesting expressions. Then build a recursion or loop to do the same operation for each match.

To get data that is not included in brackets, use the same regularity in the replacement function:

 preg_replace("/\[(((?>[^\[\]]+)|(?R))*)\]/", "", "text0[text01][text02[text023[text0234]]]"); //outputs: text0