3v4l.org

run code in 300+ PHP versions simultaneously
<?php function replace2($matches, $replacements, $frame) { if (!isset($matches[0]) || !is_array($matches[0]) || empty($matches[0])) { return []; } $result = []; foreach ($matches[0] as $value) { if (isset($replacements[$value])) { foreach ($replacements[$value] as $v) { $tmp = str_replace($value, $v, $frame); $result[$tmp] = null; array_shift($matches); replace2($matches, $replacements, $frame); } } } return $result; } function doStuff($frame) { $replacements = [ '{childName}' => [], '{favoriteFood}' => ['pizza', 'soup', 'burger'], '{favoriteToy}' => ['doll', 'teddy bear', 'truck', 'computer'] ]; preg_match_all('/'.implode('|', array_keys($replacements)).'/', $frame, $matches); if (count($matches[0]) < 1) { return [$frame => null]; } return replace2($matches, $replacements, $frame); } $frames = [ '{childName} loves to eat {favoriteFood} and play with his or her {favoriteToy}', '{childName} loves to eat {favoriteFood}', '{childName} loves to play' ]; foreach ($frames as $frame) { var_dump(doStuff($frame)); echo "\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 11
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 11
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/aPshc
function name:  (null)
number of ops:  13
compiled vars:  !0 = $frames, !1 = $frame
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   ASSIGN                                                   !0, <array>
   41     1      > FE_RESET_R                                       $3      !0, ->11
          2    > > FE_FETCH_R                                               $3, !1, ->11
   42     3    >   INIT_FCALL                                               'var_dump'
          4        INIT_FCALL                                               'dostuff'
          5        SEND_VAR                                                 !1
          6        DO_FCALL                                      0  $4      
          7        SEND_VAR                                                 $4
          8        DO_ICALL                                                 
   43     9        ECHO                                                     '%0A'
   41    10      > JMP                                                      ->2
         11    >   FE_FREE                                                  $3
   44    12      > RETURN                                                   1

Function replace2:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 6, Position 2 = 10
Branch analysis from position: 6
2 jumps found. (Code = 47) Position 1 = 11, Position 2 = 13
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 15
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 77) Position 1 = 18, Position 2 = 43
Branch analysis from position: 18
2 jumps found. (Code = 78) Position 1 = 19, Position 2 = 43
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 42
Branch analysis from position: 21
2 jumps found. (Code = 77) Position 1 = 23, Position 2 = 41
Branch analysis from position: 23
2 jumps found. (Code = 78) Position 1 = 24, Position 2 = 41
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 41
Branch analysis from position: 42
Branch analysis from position: 43
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 43
Branch analysis from position: 13
Branch analysis from position: 10
filename:       /in/aPshc
function name:  replace2
number of ops:  46
compiled vars:  !0 = $matches, !1 = $replacements, !2 = $frame, !3 = $result, !4 = $value, !5 = $v, !6 = $tmp
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
    3     3        ISSET_ISEMPTY_DIM_OBJ                         0  ~7      !0, 0
          4        BOOL_NOT                                         ~8      ~7
          5      > JMPNZ_EX                                         ~8      ~8, ->10
          6    >   FETCH_DIM_R                                      ~9      !0, 0
          7        TYPE_CHECK                                  128  ~10     ~9
          8        BOOL_NOT                                         ~11     ~10
          9        BOOL                                             ~8      ~11
         10    > > JMPNZ_EX                                         ~8      ~8, ->13
         11    >   ISSET_ISEMPTY_DIM_OBJ                         1  ~12     !0, 0
         12        BOOL                                             ~8      ~12
         13    > > JMPZ                                                     ~8, ->15
    4    14    > > RETURN                                                   <array>
    6    15    >   ASSIGN                                                   !3, <array>
    7    16        FETCH_DIM_R                                      ~14     !0, 0
         17      > FE_RESET_R                                       $15     ~14, ->43
         18    > > FE_FETCH_R                                               $15, !4, ->43
    8    19    >   ISSET_ISEMPTY_DIM_OBJ                         0          !1, !4
         20      > JMPZ                                                     ~16, ->42
    9    21    >   FETCH_DIM_R                                      ~17     !1, !4
         22      > FE_RESET_R                                       $18     ~17, ->41
         23    > > FE_FETCH_R                                               $18, !5, ->41
   10    24    >   INIT_FCALL                                               'str_replace'
         25        SEND_VAR                                                 !4
         26        SEND_VAR                                                 !5
         27        SEND_VAR                                                 !2
         28        DO_ICALL                                         $19     
         29        ASSIGN                                                   !6, $19
   11    30        ASSIGN_DIM                                               !3, !6
         31        OP_DATA                                                  null
   12    32        INIT_FCALL                                               'array_shift'
         33        SEND_REF                                                 !0
         34        DO_ICALL                                                 
   13    35        INIT_FCALL_BY_NAME                                       'replace2'
         36        SEND_VAR_EX                                              !0
         37        SEND_VAR_EX                                              !1
         38        SEND_VAR_EX                                              !2
         39        DO_FCALL                                      0          
    9    40      > JMP                                                      ->23
         41    >   FE_FREE                                                  $18
    7    42    > > JMP                                                      ->18
         43    >   FE_FREE                                                  $15
   17    44      > RETURN                                                   !3
   18    45*     > RETURN                                                   null

End of function replace2

Function dostuff:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 22
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/aPshc
function name:  doStuff
number of ops:  29
compiled vars:  !0 = $frame, !1 = $replacements, !2 = $matches
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   RECV                                             !0      
   21     1        ASSIGN                                                   !1, <array>
   27     2        INIT_FCALL                                               'preg_match_all'
          3        INIT_FCALL                                               'implode'
          4        SEND_VAL                                                 '%7C'
          5        INIT_FCALL                                               'array_keys'
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $4      
          8        SEND_VAR                                                 $4
          9        DO_ICALL                                         $5      
         10        CONCAT                                           ~6      '%2F', $5
         11        CONCAT                                           ~7      ~6, '%2F'
         12        SEND_VAL                                                 ~7
         13        SEND_VAR                                                 !0
         14        SEND_REF                                                 !2
         15        DO_ICALL                                                 
   28    16        FETCH_DIM_R                                      ~9      !2, 0
         17        COUNT                                            ~10     ~9
         18        IS_SMALLER                                               ~10, 1
         19      > JMPZ                                                     ~11, ->22
   29    20    >   INIT_ARRAY                                       ~12     null, !0
         21      > RETURN                                                   ~12
   32    22    >   INIT_FCALL                                               'replace2'
         23        SEND_VAR                                                 !2
         24        SEND_VAR                                                 !1
         25        SEND_VAR                                                 !0
         26        DO_FCALL                                      0  $13     
         27      > RETURN                                                   $13
   33    28*     > RETURN                                                   null

End of function dostuff

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
193.95 ms | 1411 KiB | 27 Q