3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str = <<<'STR' Group test { Group test1 { #-# Type Val 1 typ1 10 2 typ2 10 3 typ3 10 } Group test2 { #-# Type Val 1 typ1 10 2 typ2 10 } } STR; $pattern = <<<'REGEX' ~ (?(DEFINE) (?<group> Group \s+ \g<groupName> \s* { \s* (?: \g<group> (?: \s+ \g<group>)* | \g<line> (?: \h*+ \s+ \g<line> )* )? \s* } ) (?<groupName> \w+ ) (?<line> \g<value> (?: \h+ \g<value>)* ) (?<value> [^\s{}]+ ) ) \A \s* \g<group> \s* \z (*:MAIN_GROUP) ~x REGEX; function group2array($group) { $ret = []; $index = 0; // this pattern extracts the group name and the group content preg_match( '~Group \s+ (?<name> \w+ ) \s* { \s* (?<content> .* \S ) \s* } ~sx', $group, $match ); $name = $match['name']; $content = $match['content']; $ret[$name] = []; preg_match_all( // this one extracts groups or lines of values '~ \s* (?<group> Group \s+ \w+ \s* { [^{}]*+ (?: \g<group> [^{}]* )*+ } ) | \s* (?<line> [^\s{}]+ (?: \h+ [^\s{}]+ )* ) ~x', $content, $matches, PREG_SET_ORDER ); foreach($matches as $m) { if (isset($m['line'])) { $ret[$name][$index++] = preg_split('~\s+~', $m['line']); } else { $group = group2array($m['group']); $ret[$name][key($group)] = current($group); } } return $ret; } if ( preg_match($pattern, $str) ) { // check the string syntax before parsing $result = group2array($str); echo json_encode($result), PHP_EOL, print_r($result, true), PHP_EOL; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 22
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
filename:       /in/1QhM1
function name:  (null)
number of ops:  23
compiled vars:  !0 = $str, !1 = $pattern, !2 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, 'Group+test%0A%7B%0A++++Group+test1%0A++++%7B%0A++++++++%23-%23+Type++++Val%0A++++++++1+++typ1++++10%0A++++++++2+++typ2++++10%0A++++++++3+++typ3++++10%0A++++%7D%0A++++%0A++++Group+test2%0A++++%7B%0A++++++++%23-%23+Type++++Val%0A++++++++1+++typ1++++10%0A++++++++2+++typ2++++10%0A++++%7D%0A%7D'
   23     1        ASSIGN                                                   !1, '%7E%0A%28%3F%28DEFINE%29%0A++++%28%3F%3Cgroup%3E%0A++++++++Group+%5Cs%2B+%5Cg%3CgroupName%3E+%5Cs%2A+%7B+%5Cs%2A%0A++++++++++++%28%3F%3A%0A++++++++++++++++%5Cg%3Cgroup%3E+%28%3F%3A+%5Cs%2B+%5Cg%3Cgroup%3E%29%2A%0A++++++++++++++%7C%0A++++++++++++++++%5Cg%3Cline%3E+%28%3F%3A+%5Ch%2A%2B+%5Cs%2B+%5Cg%3Cline%3E+%29%2A%0A++++++++++++%29%3F%0A++++++++++++%5Cs%2A%0A++++++++%7D+%0A++++%29%0A++++%0A++++%28%3F%3CgroupName%3E+%5Cw%2B+%29%0A++++%28%3F%3Cline%3E+%5Cg%3Cvalue%3E+%28%3F%3A+%5Ch%2B+%5Cg%3Cvalue%3E%29%2A+%29%0A++++%28%3F%3Cvalue%3E+%5B%5E%5Cs%7B%7D%5D%2B+%29%0A%29%0A%0A%5CA+%5Cs%2A+%5Cg%3Cgroup%3E+%5Cs%2A+%5Cz+%28%2A%3AMAIN_GROUP%29+%0A%7Ex'
   81     2        INIT_FCALL                                               'preg_match'
          3        SEND_VAR                                                 !1
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $5      
          6      > JMPZ                                                     $5, ->22
   82     7    >   INIT_FCALL                                               'group2array'
          8        SEND_VAR                                                 !0
          9        DO_FCALL                                      0  $6      
         10        ASSIGN                                                   !2, $6
   83    11        INIT_FCALL                                               'json_encode'
         12        SEND_VAR                                                 !2
         13        DO_ICALL                                         $8      
         14        ECHO                                                     $8
         15        ECHO                                                     '%0A'
   84    16        INIT_FCALL                                               'print_r'
         17        SEND_VAR                                                 !2
         18        SEND_VAL                                                 <true>
         19        DO_ICALL                                         $9      
         20        ECHO                                                     $9
         21        ECHO                                                     '%0A'
   85    22    > > RETURN                                                   1

Function group2array:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 21, Position 2 = 50
Branch analysis from position: 21
2 jumps found. (Code = 78) Position 1 = 22, Position 2 = 50
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 34
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 49
Branch analysis from position: 49
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
Branch analysis from position: 50
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 50
filename:       /in/1QhM1
function name:  group2array
number of ops:  53
compiled vars:  !0 = $group, !1 = $ret, !2 = $index, !3 = $match, !4 = $name, !5 = $content, !6 = $matches, !7 = $m
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   RECV                                             !0      
   47     1        ASSIGN                                                   !1, <array>
   48     2        ASSIGN                                                   !2, 0
   51     3        INIT_FCALL                                               'preg_match'
   52     4        SEND_VAL                                                 '%7EGroup+%5Cs%2B+%28%3F%3Cname%3E+%5Cw%2B+%29+%5Cs%2A+%7B+%5Cs%2A+%28%3F%3Ccontent%3E+.%2A+%5CS+%29+%5Cs%2A+%7D+%7Esx'
   53     5        SEND_VAR                                                 !0
          6        SEND_REF                                                 !3
          7        DO_ICALL                                                 
   55     8        FETCH_DIM_R                                      ~11     !3, 'name'
          9        ASSIGN                                                   !4, ~11
   56    10        FETCH_DIM_R                                      ~13     !3, 'content'
         11        ASSIGN                                                   !5, ~13
   58    12        ASSIGN_DIM                                               !1, !4
         13        OP_DATA                                                  <array>
   60    14        INIT_FCALL                                               'preg_match_all'
   61    15        SEND_VAL                                                 '%7E%0A+++++++++++%5Cs%2A+%28%3F%3Cgroup%3E+Group+%5Cs%2B+%5Cw%2B+%5Cs%2A+%7B+%5B%5E%7B%7D%5D%2A%2B+%28%3F%3A+%5Cg%3Cgroup%3E+%5B%5E%7B%7D%5D%2A+%29%2A%2B+%7D+%29%0A++++++++++%7C+%0A+++++++++++%5Cs%2A+%28%3F%3Cline%3E+%5B%5E%5Cs%7B%7D%5D%2B+%28%3F%3A+%5Ch%2B+%5B%5E%5Cs%7B%7D%5D%2B+%29%2A+%29%0A++++++++%7Ex'
   66    16        SEND_VAR                                                 !5
         17        SEND_REF                                                 !6
         18        SEND_VAL                                                 2
         19        DO_ICALL                                                 
   69    20      > FE_RESET_R                                       $17     !6, ->50
         21    > > FE_FETCH_R                                               $17, !7, ->50
   70    22    >   ISSET_ISEMPTY_DIM_OBJ                         0          !7, 'line'
         23      > JMPZ                                                     ~18, ->34
   71    24    >   POST_INC                                         ~20     !2
         25        INIT_FCALL                                               'preg_split'
         26        SEND_VAL                                                 '%7E%5Cs%2B%7E'
         27        FETCH_DIM_R                                      ~22     !7, 'line'
         28        SEND_VAL                                                 ~22
         29        DO_ICALL                                         $23     
         30        FETCH_DIM_W                                      $19     !1, !4
         31        ASSIGN_DIM                                               $19, ~20
         32        OP_DATA                                                  $23
         33      > JMP                                                      ->49
   73    34    >   INIT_FCALL_BY_NAME                                       'group2array'
         35        CHECK_FUNC_ARG                                           
         36        FETCH_DIM_FUNC_ARG                               $24     !7, 'group'
         37        SEND_FUNC_ARG                                            $24
         38        DO_FCALL                                      0  $25     
         39        ASSIGN                                                   !0, $25
   74    40        INIT_FCALL                                               'key'
         41        SEND_VAR                                                 !0
         42        DO_ICALL                                         $28     
         43        INIT_FCALL                                               'current'
         44        SEND_VAR                                                 !0
         45        DO_ICALL                                         $30     
         46        FETCH_DIM_W                                      $27     !1, !4
         47        ASSIGN_DIM                                               $27, $28
         48        OP_DATA                                                  $30
   69    49    > > JMP                                                      ->21
         50    >   FE_FREE                                                  $17
   78    51      > RETURN                                                   !1
   79    52*     > RETURN                                                   null

End of function group2array

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
136.18 ms | 1411 KiB | 28 Q