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()) { $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"; $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){ echo "{$key} -> {$contextCondition[$key]} -> {$contextValues[$key]}\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 15, Position 2 = 22
Branch analysis from position: 15
2 jumps found. (Code = 78) Position 1 = 16, Position 2 = 22
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 22
2 jumps found. (Code = 77) Position 1 = 25, Position 2 = 39
Branch analysis from position: 25
2 jumps found. (Code = 78) Position 1 = 26, Position 2 = 39
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 31
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
Branch analysis from position: 39
2 jumps found. (Code = 77) Position 1 = 41, Position 2 = 52
Branch analysis from position: 41
2 jumps found. (Code = 78) Position 1 = 42, Position 2 = 52
Branch analysis from position: 42
1 jumps found. (Code = 42) Position 1 = 41
Branch analysis from position: 41
Branch analysis from position: 52
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 52
Branch analysis from position: 39
Branch analysis from position: 22
filename:       /in/LGNhV
function name:  (null)
number of ops:  54
compiled vars:  !0 = $str, !1 = $arrContext, !2 = $arrMatches, !3 = $contextKeys, !4 = $contextCondition, !5 = $contextValues, !6 = $i, !7 = $key, !8 = $value
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'
   12     1        ASSIGN                                                   !1, <array>
   18     2        ECHO                                                     '%0A'
   20     3        ASSIGN                                                   !2, <array>
   21     4        INIT_FCALL                                               'preg_match_all'
          5        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'
          6        SEND_VAR                                                 !0
          7        SEND_REF                                                 !2
          8        DO_ICALL                                                 
   23     9        FETCH_DIM_R                                      ~13     !2, 1
         10        ASSIGN                                                   !3, ~13
   24    11        ASSIGN                                                   !4, <array>
   25    12        ASSIGN                                                   !5, <array>
   27    13        ASSIGN                                                   !6, 0
   28    14      > FE_RESET_R                                       $18     !3, ->22
         15    > > FE_FETCH_R                                               $18, !7, ->22
         16    >   FETCH_DIM_R                                      ~19     !2, 0
         17        FETCH_DIM_R                                      ~20     ~19, !6
         18        INIT_ARRAY                                       ~21     ~20, !7
         19        ASSIGN_OP                                     1          !4, ~21
         20        PRE_INC                                                  !6
         21      > JMP                                                      ->15
         22    >   FE_FREE                                                  $18
   30    23        ASSIGN                                                   !6, 0
   31    24      > FE_RESET_R                                       $25     !3, ->39
         25    > > FE_FETCH_R                                               $25, !7, ->39
         26    >   FETCH_DIM_R                                      ~26     !1, !7
         27        BOOL                                             ~27     ~26
         28      > JMPZ                                                     ~27, ->31
         29    >   QM_ASSIGN                                        ~28     2
         30      > JMP                                                      ->32
         31    >   QM_ASSIGN                                        ~28     3
         32    >   ASSIGN                                                   !8, ~28
         33        FETCH_DIM_R                                      ~30     !2, !8
         34        FETCH_DIM_R                                      ~31     ~30, !6
         35        INIT_ARRAY                                       ~32     ~31, !7
         36        ASSIGN_OP                                     1          !5, ~32
         37        PRE_INC                                                  !6
         38      > JMP                                                      ->25
         39    >   FE_FREE                                                  $25
   34    40      > FE_RESET_R                                       $35     !3, ->52
         41    > > FE_FETCH_R                                               $35, !7, ->52
   35    42    >   ROPE_INIT                                     6  ~39     !7
         43        ROPE_ADD                                      1  ~39     ~39, '+-%3E+'
         44        FETCH_DIM_R                                      ~36     !4, !7
         45        ROPE_ADD                                      2  ~39     ~39, ~36
         46        ROPE_ADD                                      3  ~39     ~39, '+-%3E+'
         47        FETCH_DIM_R                                      ~37     !5, !7
         48        ROPE_ADD                                      4  ~39     ~39, ~37
         49        ROPE_END                                      5  ~38     ~39, '%0A'
         50        ECHO                                                     ~38
   34    51      > JMP                                                      ->41
         52    >   FE_FREE                                                  $35
   36    53      > RETURN                                                   1

Function interpolatestring:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 11
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 11
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/LGNhV
function name:  interpolateString
number of ops:  18
compiled vars:  !0 = $message, !1 = $context, !2 = $replace, !3 = $val, !4 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
    5     2        ASSIGN                                                   !2, <array>
    6     3      > FE_RESET_R                                       $6      !1, ->11
          4    > > FE_FETCH_R                                       ~7      $6, !3, ->11
          5    >   ASSIGN                                                   !4, ~7
    7     6        CONCAT                                           ~9      '%7B', !4
          7        CONCAT                                           ~10     ~9, '%7D'
          8        ASSIGN_DIM                                               !2, ~10
          9        OP_DATA                                                  !3
    6    10      > JMP                                                      ->4
         11    >   FE_FREE                                                  $6
    9    12        INIT_FCALL                                               'strtr'
         13        SEND_VAR                                                 !0
         14        SEND_VAR                                                 !2
         15        DO_ICALL                                         $12     
         16      > RETURN                                                   $12
   10    17*     > RETURN                                                   null

End of function interpolatestring

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.96 ms | 949 KiB | 18 Q