3v4l.org

run code in 300+ PHP versions simultaneously
<?php echo Templation::Run("hello {{world}} how are {{you}}", $data = array('world'=>'WORLD!', 'you'=>'YOU!')); class Templation { /** * Mark the templated variable with a special class in html so javascript can use it also. * @param $match * @param $replace_with * @return string */ private static function enclose($match, $replace_with){ return '<span class="Templation" title='.json_encode(htmlentities($match)).' data-Templation='.json_encode(htmlentities($match)).'>'.$replace_with.'</span>'; } /** * Run the template through with the given data and return a result * optionally enclosing each replacement with a <span class="Templation"> * For the JavaScript version to run correctly. * @see Templation.js * * @param $template * @param $data * @return string */ public static function Run($template, $data, $enclose = true){ $result = $template; preg_match_all('/{{([^}]+?)}}/im', $template, $matches, PREG_PATTERN_ORDER); $matches = $matches[0]; foreach($matches as $i => $match){ if (strpos($match, '||') !== false){ $replace_with = self::PriorityReplace($match, $data); $result = str_replace($match, $enclose ? self::enclose($match, $replace_with) : $replace_with, $result); }else{ $match2 = preg_replace("/(\\{\\{|\\}\\})/", "", $match); if (isset($data[$match2])){ $result = str_replace($match, $enclose ? self::enclose($match, $data[$match2]) : $data[$match2], $result); }else{ $result = str_replace($match, $enclose ? self::enclose($match, $match) : $match, $result); } } } return $result; } /** * Do a Priority Replacement based on the fact that there is || as a delimiter * This means that currently literal text with a '||' is not supported * for example {{var5||var4||var3||'Hello || World'}} will break it. (too many features would slow it down) * * @param $match * @param $data * @return mixed|string */ public static function PriorityReplace($match, $data){ $match = preg_replace("/(\\{\\{|\\}\\})/", "", $match); $p = explode('||', $match); //var_dump($p); for($i = 0; $i < count($p); ++$i){ if (strpos($p[$i], '\'') !== false){ return trim($p[$i],"'"); } else if (isset($data[$p[$i]])){ return $data[$p[$i]]; } } return $match; } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HIs0u
function name:  (null)
number of ops:  7
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   INIT_STATIC_METHOD_CALL                                  'Templation', 'Run'
          1        SEND_VAL_EX                                              'hello+%7B%7Bworld%7D%7D+how+are+%7B%7Byou%7D%7D'
          2        ASSIGN                                           ~1      !0, <array>
          3        SEND_VAL_EX                                              ~1
          4        DO_FCALL                                      0  $2      
          5        ECHO                                                     $2
   71     6      > RETURN                                                   1

Class Templation:
Function enclose:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HIs0u
function name:  enclose
number of ops:  22
compiled vars:  !0 = $match, !1 = $replace_with
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   15     2        INIT_FCALL                                               'json_encode'
          3        INIT_FCALL                                               'htmlentities'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $2      
          6        SEND_VAR                                                 $2
          7        DO_ICALL                                         $3      
          8        CONCAT                                           ~4      '%3Cspan+class%3D%22Templation%22+title%3D', $3
          9        CONCAT                                           ~5      ~4, '+data-Templation%3D'
         10        INIT_FCALL                                               'json_encode'
         11        INIT_FCALL                                               'htmlentities'
         12        SEND_VAR                                                 !0
         13        DO_ICALL                                         $6      
         14        SEND_VAR                                                 $6
         15        DO_ICALL                                         $7      
         16        CONCAT                                           ~8      ~5, $7
         17        CONCAT                                           ~9      ~8, '%3E'
         18        CONCAT                                           ~10     ~9, !1
         19        CONCAT                                           ~11     ~10, '%3C%2Fspan%3E'
         20      > RETURN                                                   ~11
   16    21*     > RETURN                                                   null

End of function enclose

Function run:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 81
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 81
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 41
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 35
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 80
Branch analysis from position: 80
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 80
Branch analysis from position: 80
Branch analysis from position: 41
2 jumps found. (Code = 43) Position 1 = 49, Position 2 = 66
Branch analysis from position: 49
2 jumps found. (Code = 43) Position 1 = 52, Position 2 = 59
Branch analysis from position: 52
1 jumps found. (Code = 42) Position 1 = 61
Branch analysis from position: 61
1 jumps found. (Code = 42) Position 1 = 80
Branch analysis from position: 80
Branch analysis from position: 59
1 jumps found. (Code = 42) Position 1 = 80
Branch analysis from position: 80
Branch analysis from position: 66
2 jumps found. (Code = 43) Position 1 = 69, Position 2 = 75
Branch analysis from position: 69
1 jumps found. (Code = 42) Position 1 = 76
Branch analysis from position: 76
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 75
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 81
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 81
filename:       /in/HIs0u
function name:  Run
number of ops:  84
compiled vars:  !0 = $template, !1 = $data, !2 = $enclose, !3 = $result, !4 = $matches, !5 = $match, !6 = $i, !7 = $replace_with, !8 = $match2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <true>
   29     3        ASSIGN                                                   !3, !0
   30     4        INIT_FCALL                                               'preg_match_all'
          5        SEND_VAL                                                 '%2F%7B%7B%28%5B%5E%7D%5D%2B%3F%29%7D%7D%2Fim'
          6        SEND_VAR                                                 !0
          7        SEND_REF                                                 !4
          8        SEND_VAL                                                 1
          9        DO_ICALL                                                 
   31    10        FETCH_DIM_R                                      ~11     !4, 0
         11        ASSIGN                                                   !4, ~11
   32    12      > FE_RESET_R                                       $13     !4, ->81
         13    > > FE_FETCH_R                                       ~14     $13, !5, ->81
         14    >   ASSIGN                                                   !6, ~14
   33    15        INIT_FCALL                                               'strpos'
         16        SEND_VAR                                                 !5
         17        SEND_VAL                                                 '%7C%7C'
         18        DO_ICALL                                         $16     
         19        TYPE_CHECK                                  1018          $16
         20      > JMPZ                                                     ~17, ->41
   34    21    >   INIT_STATIC_METHOD_CALL                                  'PriorityReplace'
         22        SEND_VAR_EX                                              !5
         23        SEND_VAR_EX                                              !1
         24        DO_FCALL                                      0  $18     
         25        ASSIGN                                                   !7, $18
   35    26        INIT_FCALL                                               'str_replace'
         27        SEND_VAR                                                 !5
         28      > JMPZ                                                     !2, ->35
         29    >   INIT_STATIC_METHOD_CALL                                  'enclose'
         30        SEND_VAR                                                 !5
         31        SEND_VAR                                                 !7
         32        DO_FCALL                                      0  $20     
         33        QM_ASSIGN                                        ~21     $20
         34      > JMP                                                      ->36
         35    >   QM_ASSIGN                                        ~21     !7
         36    >   SEND_VAL                                                 ~21
         37        SEND_VAR                                                 !3
         38        DO_ICALL                                         $22     
         39        ASSIGN                                                   !3, $22
         40      > JMP                                                      ->80
   37    41    >   INIT_FCALL                                               'preg_replace'
         42        SEND_VAL                                                 '%2F%28%5C%7B%5C%7B%7C%5C%7D%5C%7D%29%2F'
         43        SEND_VAL                                                 ''
         44        SEND_VAR                                                 !5
         45        DO_ICALL                                         $24     
         46        ASSIGN                                                   !8, $24
   38    47        ISSET_ISEMPTY_DIM_OBJ                         0          !1, !8
         48      > JMPZ                                                     ~26, ->66
   39    49    >   INIT_FCALL                                               'str_replace'
         50        SEND_VAR                                                 !5
         51      > JMPZ                                                     !2, ->59
         52    >   INIT_STATIC_METHOD_CALL                                  'enclose'
         53        SEND_VAR                                                 !5
         54        FETCH_DIM_R                                      ~27     !1, !8
         55        SEND_VAL                                                 ~27
         56        DO_FCALL                                      0  $28     
         57        QM_ASSIGN                                        ~29     $28
         58      > JMP                                                      ->61
         59    >   FETCH_DIM_R                                      ~30     !1, !8
         60        QM_ASSIGN                                        ~29     ~30
         61    >   SEND_VAL                                                 ~29
         62        SEND_VAR                                                 !3
         63        DO_ICALL                                         $31     
         64        ASSIGN                                                   !3, $31
         65      > JMP                                                      ->80
   41    66    >   INIT_FCALL                                               'str_replace'
         67        SEND_VAR                                                 !5
         68      > JMPZ                                                     !2, ->75
         69    >   INIT_STATIC_METHOD_CALL                                  'enclose'
         70        SEND_VAR                                                 !5
         71        SEND_VAR                                                 !5
         72        DO_FCALL                                      0  $33     
         73        QM_ASSIGN                                        ~34     $33
         74      > JMP                                                      ->76
         75    >   QM_ASSIGN                                        ~34     !5
         76    >   SEND_VAL                                                 ~34
         77        SEND_VAR                                                 !3
         78        DO_ICALL                                         $35     
         79        ASSIGN                                                   !3, $35
   32    80    > > JMP                                                      ->13
         81    >   FE_FREE                                                  $13
   45    82      > RETURN                                                   !3
   46    83*     > RETURN                                                   null

End of function run

Function priorityreplace:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
2 jumps found. (Code = 44) Position 1 = 39, Position 2 = 15
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 29
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 32, Position 2 = 35
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 35
2 jumps found. (Code = 44) Position 1 = 39, Position 2 = 15
Branch analysis from position: 39
Branch analysis from position: 15
filename:       /in/HIs0u
function name:  PriorityReplace
number of ops:  41
compiled vars:  !0 = $match, !1 = $data, !2 = $p, !3 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   58     2        INIT_FCALL                                               'preg_replace'
          3        SEND_VAL                                                 '%2F%28%5C%7B%5C%7B%7C%5C%7D%5C%7D%29%2F'
          4        SEND_VAL                                                 ''
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $4      
          7        ASSIGN                                                   !0, $4
   59     8        INIT_FCALL                                               'explode'
          9        SEND_VAL                                                 '%7C%7C'
         10        SEND_VAR                                                 !0
         11        DO_ICALL                                         $6      
         12        ASSIGN                                                   !2, $6
   61    13        ASSIGN                                                   !3, 0
         14      > JMP                                                      ->36
   62    15    >   INIT_FCALL                                               'strpos'
         16        FETCH_DIM_R                                      ~9      !2, !3
         17        SEND_VAL                                                 ~9
         18        SEND_VAL                                                 '%27'
         19        DO_ICALL                                         $10     
         20        TYPE_CHECK                                  1018          $10
         21      > JMPZ                                                     ~11, ->29
   63    22    >   INIT_FCALL                                               'trim'
         23        FETCH_DIM_R                                      ~12     !2, !3
         24        SEND_VAL                                                 ~12
         25        SEND_VAL                                                 '%27'
         26        DO_ICALL                                         $13     
         27      > RETURN                                                   $13
         28*       JMP                                                      ->35
   65    29    >   FETCH_DIM_R                                      ~14     !2, !3
         30        ISSET_ISEMPTY_DIM_OBJ                         0          !1, ~14
         31      > JMPZ                                                     ~15, ->35
   66    32    >   FETCH_DIM_R                                      ~16     !2, !3
         33        FETCH_DIM_R                                      ~17     !1, ~16
         34      > RETURN                                                   ~17
   61    35    >   PRE_INC                                                  !3
         36    >   COUNT                                            ~19     !2
         37        IS_SMALLER                                               !3, ~19
         38      > JMPNZ                                                    ~20, ->15
   69    39    > > RETURN                                                   !0
   70    40*     > RETURN                                                   null

End of function priorityreplace

End of class Templation.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
163.4 ms | 1412 KiB | 29 Q