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]; $i = 0; foreach ($contextKeys as $key){ $contextCondition = array($key => $arrMatches[0][$i]); $i++; } foreach ($contextKeys as $key){ echo "{$key} -> {$contextCondition[$key]}\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 20
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 20
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 20
2 jumps found. (Code = 77) Position 1 = 22, Position 2 = 30
Branch analysis from position: 22
2 jumps found. (Code = 78) Position 1 = 23, Position 2 = 30
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
Branch analysis from position: 20
filename:       /in/2Z1sc
function name:  (null)
number of ops:  32
compiled vars:  !0 = $str, !1 = $arrContext, !2 = $arrMatches, !3 = $contextKeys, !4 = $i, !5 = $key, !6 = $contextCondition
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                                      ~11     !2, 1
         10        ASSIGN                                                   !3, ~11
   24    11        ASSIGN                                                   !4, 0
   25    12      > FE_RESET_R                                       $14     !3, ->20
         13    > > FE_FETCH_R                                               $14, !5, ->20
   26    14    >   FETCH_DIM_R                                      ~15     !2, 0
         15        FETCH_DIM_R                                      ~16     ~15, !4
         16        INIT_ARRAY                                       ~17     ~16, !5
         17        ASSIGN                                                   !6, ~17
   27    18        PRE_INC                                                  !4
   25    19      > JMP                                                      ->13
         20    >   FE_FREE                                                  $14
   30    21      > FE_RESET_R                                       $20     !3, ->30
         22    > > FE_FETCH_R                                               $20, !5, ->30
   31    23    >   ROPE_INIT                                     4  ~23     !5
         24        ROPE_ADD                                      1  ~23     ~23, '+-%3E+'
         25        FETCH_DIM_R                                      ~21     !6, !5
         26        ROPE_ADD                                      2  ~23     ~23, ~21
         27        ROPE_END                                      3  ~22     ~23, '%0A'
         28        ECHO                                                     ~22
   30    29      > JMP                                                      ->22
         30    >   FE_FREE                                                  $20
   32    31      > 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/2Z1sc
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:
153.38 ms | 1400 KiB | 17 Q