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++;} 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/nGQGv
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 = 40, Position 2 = 50
Branch analysis from position: 40
2 jumps found. (Code = 78) Position 1 = 41, Position 2 = 50
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
Branch analysis from position: 50
2 jumps found. (Code = 77) Position 1 = 54, Position 2 = 61
Branch analysis from position: 54
2 jumps found. (Code = 78) Position 1 = 55, Position 2 = 61
Branch analysis from position: 55
1 jumps found. (Code = 42) Position 1 = 54
Branch analysis from position: 54
Branch analysis from position: 61
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 61
Branch analysis from position: 50
Branch analysis from position: 38
Branch analysis from position: 21
filename:       /in/nGQGv
function name:  interpolateString
number of ops:  68
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
   21    39      > FE_RESET_R                                       $37     !4, ->50
         40    > > FE_FETCH_R                                               $37, !8, ->50
   22    41    >   INIT_FCALL                                               'str_replace'
         42        FETCH_DIM_R                                      ~38     !5, !8
         43        SEND_VAL                                                 ~38
         44        FETCH_DIM_R                                      ~39     !6, !8
         45        SEND_VAL                                                 ~39
         46        SEND_VAR                                                 !3
         47        DO_ICALL                                         $40     
         48        ASSIGN                                                   !3, $40
   21    49      > JMP                                                      ->40
         50    >   FE_FREE                                                  $37
   25    51        ASSIGN                                                   !0, !3
   26    52        ASSIGN                                                   !11, <array>
   27    53      > FE_RESET_R                                       $44     !1, ->61
         54    > > FE_FETCH_R                                       ~45     $44, !12, ->61
         55    >   ASSIGN                                                   !8, ~45
   28    56        CONCAT                                           ~47     '%7B', !8
         57        CONCAT                                           ~48     ~47, '%7D'
         58        ASSIGN_DIM                                               !11, ~48
         59        OP_DATA                                                  !12
   27    60      > JMP                                                      ->54
         61    >   FE_FREE                                                  $44
   30    62        INIT_FCALL                                               'strtr'
         63        SEND_VAR                                                 !0
         64        SEND_VAR                                                 !11
         65        DO_ICALL                                         $50     
         66      > RETURN                                                   $50
   31    67*     > RETURN                                                   null

End of function interpolatestring

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
153.65 ms | 1411 KiB | 20 Q