3v4l.org

run code in 300+ PHP versions simultaneously
<?php function template($template, $vals) { $matches = array(); $regex = "/\{\{(\S+)\}\}/e"; $rendered = $template; // in case no matches are found preg_match_all($regex, $template, $matches, PREG_OFFSET_CAPTURE, 3); foreach ($matches[0] as $found) { $block = $found[0]; $offset = $found[0]; $k = str_replace('{{', '', $block); $k = str_replace('}-}', '', $k); $rendered = preg_replace($regex, '$vals[\\1]', $template); } return $rendered; } function template2($template, $vals) { $matches = array(); $regex = "/\{\{(\S+)\}\}/e"; $rendered = $template; // in case no matches are found preg_match_all($regex, $template, $matches, PREG_OFFSET_CAPTURE, 3); foreach ($matches[0] as $found) { $block = $found[0]; $offset = $found[0]; $k = str_replace('{{', '', $block); $k = str_replace('}-}', '', $k); $rendered = preg_replace_callback($regex, function($matches) { return $matches[0]; }, $template); } return $rendered; } $template = "test {{ME}}."; $vals = array('ME' => 'beep'); echo template($template, $vals);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/CgLXW
function name:  (null)
number of ops:  8
compiled vars:  !0 = $template, !1 = $vals
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   ASSIGN                                                   !0, 'test+%7B%7BME%7D%7D.'
   48     1        ASSIGN                                                   !1, <array>
   50     2        INIT_FCALL                                               'template'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_FCALL                                      0  $4      
          6        ECHO                                                     $4
          7      > RETURN                                                   1

Function template:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 14, Position 2 = 38
Branch analysis from position: 14
2 jumps found. (Code = 78) Position 1 = 15, Position 2 = 38
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 38
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 38
filename:       /in/CgLXW
function name:  template
number of ops:  41
compiled vars:  !0 = $template, !1 = $vals, !2 = $matches, !3 = $regex, !4 = $rendered, !5 = $found, !6 = $block, !7 = $offset, !8 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    5     2        ASSIGN                                                   !2, <array>
    6     3        ASSIGN                                                   !3, '%2F%5C%7B%5C%7B%28%5CS%2B%29%5C%7D%5C%7D%2Fe'
    7     4        ASSIGN                                                   !4, !0
    9     5        INIT_FCALL                                               'preg_match_all'
          6        SEND_VAR                                                 !3
          7        SEND_VAR                                                 !0
          8        SEND_REF                                                 !2
          9        SEND_VAL                                                 256
         10        SEND_VAL                                                 3
         11        DO_ICALL                                                 
   11    12        FETCH_DIM_R                                      ~13     !2, 0
         13      > FE_RESET_R                                       $14     ~13, ->38
         14    > > FE_FETCH_R                                               $14, !5, ->38
   12    15    >   FETCH_DIM_R                                      ~15     !5, 0
         16        ASSIGN                                                   !6, ~15
   13    17        FETCH_DIM_R                                      ~17     !5, 0
         18        ASSIGN                                                   !7, ~17
   15    19        INIT_FCALL                                               'str_replace'
         20        SEND_VAL                                                 '%7B%7B'
         21        SEND_VAL                                                 ''
         22        SEND_VAR                                                 !6
         23        DO_ICALL                                         $19     
         24        ASSIGN                                                   !8, $19
   16    25        INIT_FCALL                                               'str_replace'
         26        SEND_VAL                                                 '%7D-%7D'
         27        SEND_VAL                                                 ''
         28        SEND_VAR                                                 !8
         29        DO_ICALL                                         $21     
         30        ASSIGN                                                   !8, $21
   18    31        INIT_FCALL                                               'preg_replace'
         32        SEND_VAR                                                 !3
         33        SEND_VAL                                                 '%24vals%5B%5C1%5D'
         34        SEND_VAR                                                 !0
         35        DO_ICALL                                         $23     
         36        ASSIGN                                                   !4, $23
   11    37      > JMP                                                      ->14
         38    >   FE_FREE                                                  $14
   21    39      > RETURN                                                   !4
   22    40*     > RETURN                                                   null

End of function template

Function template2:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 14, Position 2 = 39
Branch analysis from position: 14
2 jumps found. (Code = 78) Position 1 = 15, Position 2 = 39
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 39
filename:       /in/CgLXW
function name:  template2
number of ops:  42
compiled vars:  !0 = $template, !1 = $vals, !2 = $matches, !3 = $regex, !4 = $rendered, !5 = $found, !6 = $block, !7 = $offset, !8 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   26     2        ASSIGN                                                   !2, <array>
   27     3        ASSIGN                                                   !3, '%2F%5C%7B%5C%7B%28%5CS%2B%29%5C%7D%5C%7D%2Fe'
   28     4        ASSIGN                                                   !4, !0
   30     5        INIT_FCALL                                               'preg_match_all'
          6        SEND_VAR                                                 !3
          7        SEND_VAR                                                 !0
          8        SEND_REF                                                 !2
          9        SEND_VAL                                                 256
         10        SEND_VAL                                                 3
         11        DO_ICALL                                                 
   32    12        FETCH_DIM_R                                      ~13     !2, 0
         13      > FE_RESET_R                                       $14     ~13, ->39
         14    > > FE_FETCH_R                                               $14, !5, ->39
   33    15    >   FETCH_DIM_R                                      ~15     !5, 0
         16        ASSIGN                                                   !6, ~15
   34    17        FETCH_DIM_R                                      ~17     !5, 0
         18        ASSIGN                                                   !7, ~17
   36    19        INIT_FCALL                                               'str_replace'
         20        SEND_VAL                                                 '%7B%7B'
         21        SEND_VAL                                                 ''
         22        SEND_VAR                                                 !6
         23        DO_ICALL                                         $19     
         24        ASSIGN                                                   !8, $19
   37    25        INIT_FCALL                                               'str_replace'
         26        SEND_VAL                                                 '%7D-%7D'
         27        SEND_VAL                                                 ''
         28        SEND_VAR                                                 !8
         29        DO_ICALL                                         $21     
         30        ASSIGN                                                   !8, $21
   39    31        INIT_FCALL                                               'preg_replace_callback'
         32        SEND_VAR                                                 !3
         33        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FCgLXW%3A39%240'
   41    34        SEND_VAL                                                 ~23
         35        SEND_VAR                                                 !0
         36        DO_ICALL                                         $24     
   39    37        ASSIGN                                                   !4, $24
   32    38      > JMP                                                      ->14
         39    >   FE_FREE                                                  $14
   44    40      > RETURN                                                   !4
   45    41*     > RETURN                                                   null

End of function template2

Function %00%7Bclosure%7D%2Fin%2FCgLXW%3A39%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/CgLXW
function name:  {closure}
number of ops:  4
compiled vars:  !0 = $matches
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   RECV                                             !0      
   40     1        FETCH_DIM_R                                      ~1      !0, 0
          2      > RETURN                                                   ~1
   41     3*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FCgLXW%3A39%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162.11 ms | 1407 KiB | 22 Q