3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str = 'Showing results [program=for {program} programs|No pogram at all] [province=in {province}] [degree=with degree of {degree}]'; function interpolateString($message, array $context = array()) { $arrMatches = array(); preg_match_all("/\[(\w+)\=([^\[\]\|.*]*)?\|*([^\[\].*]*)\]/", $str, $arrMatches); $contextKeys = $arrMatches[1]; $contextCondition = array(); $contextValues = array(); $i = 0; foreach ($contextKeys as $key){ $contextCondition += array($key => $arrMatches[0][$i]); $i++;} $i = 0; foreach ($contextKeys as $key){ $value = $arrContext[$key] != false ? 2 : 3; $contextValues += array($key => $arrMatches[$value][$i]); $i++;} $str = $message; foreach ($contextKeys as $key){ $str = str_replace($contextCondition[$key], $contextValues[$key], $str); } $message = $str; $replace = array(); foreach ($context as $key => $val) { $replace['{' . $key . '}'] = $val; } return strtr($message, $replace); } $arrContext = array( 'program' => 'Nursing', 'province' => 'Ontario', 'degree' => false ); echo interpolateString($str, $arrContext); echo "\n";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/V3ETT
function name:  (null)
number of ops:  9
compiled vars:  !0 = $str, !1 = $arrContext
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, 'Showing+results+%5Bprogram%3Dfor+%7Bprogram%7D+programs%7CNo+pogram+at+all%5D+%5Bprovince%3Din+%7Bprovince%7D%5D+%5Bdegree%3Dwith+degree+of+%7Bdegree%7D%5D'
   33     1        ASSIGN                                                   !1, <array>
   38     2        INIT_FCALL                                               'interpolatestring'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_FCALL                                      0  $4      
          6        ECHO                                                     $4
   39     7        ECHO                                                     '%0A'
          8      > RETURN                                                   1

Function interpolatestring:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 14, Position 2 = 21
Branch analysis from position: 14
2 jumps found. (Code = 78) Position 1 = 15, Position 2 = 21
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 21
2 jumps found. (Code = 77) Position 1 = 24, Position 2 = 38
Branch analysis from position: 24
2 jumps found. (Code = 78) Position 1 = 25, Position 2 = 38
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 30
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 38
2 jumps found. (Code = 77) Position 1 = 41, Position 2 = 51
Branch analysis from position: 41
2 jumps found. (Code = 78) Position 1 = 42, Position 2 = 51
Branch analysis from position: 42
1 jumps found. (Code = 42) Position 1 = 41
Branch analysis from position: 41
Branch analysis from position: 51
2 jumps found. (Code = 77) Position 1 = 55, Position 2 = 62
Branch analysis from position: 55
2 jumps found. (Code = 78) Position 1 = 56, Position 2 = 62
Branch analysis from position: 56
1 jumps found. (Code = 42) Position 1 = 55
Branch analysis from position: 55
Branch analysis from position: 62
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 62
Branch analysis from position: 51
Branch analysis from position: 38
Branch analysis from position: 21
filename:       /in/V3ETT
function name:  interpolateString
number of ops:  69
compiled vars:  !0 = $message, !1 = $context, !2 = $arrMatches, !3 = $str, !4 = $contextKeys, !5 = $contextCondition, !6 = $contextValues, !7 = $i, !8 = $key, !9 = $value, !10 = $arrContext, !11 = $replace, !12 = $val
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
    7     2        ASSIGN                                                   !2, <array>
    8     3        INIT_FCALL                                               'preg_match_all'
          4        SEND_VAL                                                 '%2F%5C%5B%28%5Cw%2B%29%5C%3D%28%5B%5E%5C%5B%5C%5D%5C%7C.%2A%5D%2A%29%3F%5C%7C%2A%28%5B%5E%5C%5B%5C%5D.%2A%5D%2A%29%5C%5D%2F'
          5        SEND_VAR                                                 !3
          6        SEND_REF                                                 !2
          7        DO_ICALL                                                 
   10     8        FETCH_DIM_R                                      ~15     !2, 1
          9        ASSIGN                                                   !4, ~15
   11    10        ASSIGN                                                   !5, <array>
   12    11        ASSIGN                                                   !6, <array>
   14    12        ASSIGN                                                   !7, 0
   15    13      > FE_RESET_R                                       $20     !4, ->21
         14    > > FE_FETCH_R                                               $20, !8, ->21
         15    >   FETCH_DIM_R                                      ~21     !2, 0
         16        FETCH_DIM_R                                      ~22     ~21, !7
         17        INIT_ARRAY                                       ~23     ~22, !8
         18        ASSIGN_OP                                     1          !5, ~23
         19        PRE_INC                                                  !7
         20      > JMP                                                      ->14
         21    >   FE_FREE                                                  $20
   17    22        ASSIGN                                                   !7, 0
   18    23      > FE_RESET_R                                       $27     !4, ->38
         24    > > FE_FETCH_R                                               $27, !8, ->38
         25    >   FETCH_DIM_R                                      ~28     !10, !8
         26        BOOL                                             ~29     ~28
         27      > JMPZ                                                     ~29, ->30
         28    >   QM_ASSIGN                                        ~30     2
         29      > JMP                                                      ->31
         30    >   QM_ASSIGN                                        ~30     3
         31    >   ASSIGN                                                   !9, ~30
         32        FETCH_DIM_R                                      ~32     !2, !9
         33        FETCH_DIM_R                                      ~33     ~32, !7
         34        INIT_ARRAY                                       ~34     ~33, !8
         35        ASSIGN_OP                                     1          !6, ~34
         36        PRE_INC                                                  !7
         37      > JMP                                                      ->24
         38    >   FE_FREE                                                  $27
   20    39        ASSIGN                                                   !3, !0
   21    40      > FE_RESET_R                                       $38     !4, ->51
         41    > > FE_FETCH_R                                               $38, !8, ->51
   22    42    >   INIT_FCALL                                               'str_replace'
         43        FETCH_DIM_R                                      ~39     !5, !8
         44        SEND_VAL                                                 ~39
         45        FETCH_DIM_R                                      ~40     !6, !8
         46        SEND_VAL                                                 ~40
         47        SEND_VAR                                                 !3
         48        DO_ICALL                                         $41     
         49        ASSIGN                                                   !3, $41
   21    50      > JMP                                                      ->41
         51    >   FE_FREE                                                  $38
   25    52        ASSIGN                                                   !0, !3
   26    53        ASSIGN                                                   !11, <array>
   27    54      > FE_RESET_R                                       $45     !1, ->62
         55    > > FE_FETCH_R                                       ~46     $45, !12, ->62
         56    >   ASSIGN                                                   !8, ~46
   28    57        CONCAT                                           ~48     '%7B', !8
         58        CONCAT                                           ~49     ~48, '%7D'
         59        ASSIGN_DIM                                               !11, ~49
         60        OP_DATA                                                  !12
   27    61      > JMP                                                      ->55
         62    >   FE_FREE                                                  $45
   30    63        INIT_FCALL                                               'strtr'
         64        SEND_VAR                                                 !0
         65        SEND_VAR                                                 !11
         66        DO_ICALL                                         $51     
         67      > RETURN                                                   $51
   31    68*     > RETURN                                                   null

End of function interpolatestring

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
158.74 ms | 1407 KiB | 20 Q