3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Template { private $_template = ''; private $_assigns = array(); /** Variables with leading __ are reserved for internal using */ const VALID_VARIABLE = '(?!__)[a-zA-Z0-9_\x7f-\xff]*'; const TAG_SIGN_OPEN = '{'; const TAG_SIGN_CLOSE = '}'; public function __construct($template) { $this->_template = $template; } public function assign($name, $value) { if (!preg_match('/^' . self::VALID_VARIABLE . '$/', $name)) throw new Exception('Invalid name: "' . $name . '"'); if (is_array($value)) foreach ($value as $key_name => $val) if (!preg_match('/^' . self::VALID_VARIABLE . '$/', $key_name)) throw new Exception('Invalid key name: "' . $key_name . '"'); $this->_assigns[$name] = $value; } private function _getToken($string, $sign, $pos, &$token) { $position = strpos($string, $sign, $pos); if ($position === false) { $token = substr($string, $pos); $pos = false; } else { $token = substr($string, $pos, $position - $pos); $pos = $position + strlen($sign); } return $pos; } /** * To avoid the existence of extra variables * and overwriting assigns params * @param type $prepared_code * @param array $assigns * @return type */ private static function _execute($prepared_code, array $assigns) { $__prepared_code = $prepared_code; unset($prepared_code); $__assigns = $assigns; unset($assigns); extract($__assigns); unset($__assigns); ob_start(); eval($__prepared_code); $content = ob_get_contents(); ob_end_clean(); return $content; } public function parse() { $prepared_code = '?' . '>'; $pos = 0; while ($pos !== false) { $pos = $this->_getToken($this->_template, self::TAG_SIGN_OPEN, $pos, $token); $prepared_code .= $token; if ($pos !== false) { $pos = $this->_getToken($this->_template, self::TAG_SIGN_CLOSE, $pos, $token); if ($pos === false) throw new Exception('Template syntax error. Expected "}"'); switch (true) { case preg_match('/^' . self::VALID_VARIABLE . '$/', $token): $prepared_code .= '<?php echo ${\'' . $token . '\'} ?' . '>'; break; case preg_match('/^loop +(' . self::VALID_VARIABLE . ')$/', $token, $match): $prepared_code .= '<?php foreach (${\'' . $match[1] . '\'} as $__index => $__tmp)' . '{extract($__tmp); unset($__tmp); ?' . '>'; break; case preg_match('/^end +loop$/', $token): $prepared_code .= '<?php } ?>'; break; case preg_match('/^\%index\%$/', $token): $prepared_code .= '<?php echo $__index ?' . '>'; break; default: throw new Exception('Template syntax error. Unexpected token "' . $token . '"'); break; } } } echo $prepared_code; return self::_execute($prepared_code, $this->_assigns); } } $templateHtml = <<<END_HTML <div>{my_var}</div> {loop items} <div>{%index%}. {name}</div> {end loop} END_HTML; $template = new Template($templateHtml); $template->assign('my_var', 'test'); $template->assign('items', array( array('name' => 'First'), array('name' => 'Second') )); echo $template->parse();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bkpBp
function name:  (null)
number of ops:  17
compiled vars:  !0 = $templateHtml, !1 = $template
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  118     0  E >   ASSIGN                                                   !0, '%3Cdiv%3E%7Bmy_var%7D%3C%2Fdiv%3E%0A%7Bloop+items%7D%0A+++%3Cdiv%3E%7B%25index%25%7D.+%7Bname%7D%3C%2Fdiv%3E%0A%7Bend+loop%7D'
  125     1        NEW                                              $3      'Template'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !1, $3
  126     5        INIT_METHOD_CALL                                         !1, 'assign'
          6        SEND_VAL_EX                                              'my_var'
          7        SEND_VAL_EX                                              'test'
          8        DO_FCALL                                      0          
  127     9        INIT_METHOD_CALL                                         !1, 'assign'
         10        SEND_VAL_EX                                              'items'
  128    11        SEND_VAL_EX                                              <array>
         12        DO_FCALL                                      0          
  130    13        INIT_METHOD_CALL                                         !1, 'parse'
         14        DO_FCALL                                      0  $8      
         15        ECHO                                                     $8
         16      > RETURN                                                   1

Class Template:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bkpBp
function name:  __construct
number of ops:  4
compiled vars:  !0 = $template
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
   16     1        ASSIGN_OBJ                                               '_template'
          2        OP_DATA                                                  !0
   17     3      > RETURN                                                   null

End of function __construct

Function assign:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 14
Branch analysis from position: 8
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 33
Branch analysis from position: 16
2 jumps found. (Code = 77) Position 1 = 17, Position 2 = 32
Branch analysis from position: 17
2 jumps found. (Code = 78) Position 1 = 18, Position 2 = 32
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 31
Branch analysis from position: 25
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
Branch analysis from position: 33
filename:       /in/bkpBp
function name:  assign
number of ops:  37
compiled vars:  !0 = $name, !1 = $value, !2 = $val, !3 = $key_name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   21     2        INIT_FCALL                                               'preg_match'
          3        SEND_VAL                                                 '%2F%5E%28%3F%21__%29%5Ba-zA-Z0-9_%5Cx7f-%5Cxff%5D%2A%24%2F'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $4      
          6        BOOL_NOT                                         ~5      $4
          7      > JMPZ                                                     ~5, ->14
   22     8    >   NEW                                              $6      'Exception'
          9        CONCAT                                           ~7      'Invalid+name%3A+%22', !0
         10        CONCAT                                           ~8      ~7, '%22'
         11        SEND_VAL_EX                                              ~8
         12        DO_FCALL                                      0          
         13      > THROW                                         0          $6
   24    14    >   TYPE_CHECK                                  128          !1
         15      > JMPZ                                                     ~10, ->33
   25    16    > > FE_RESET_R                                       $11     !1, ->32
         17    > > FE_FETCH_R                                       ~12     $11, !2, ->32
         18    >   ASSIGN                                                   !3, ~12
   26    19        INIT_FCALL                                               'preg_match'
         20        SEND_VAL                                                 '%2F%5E%28%3F%21__%29%5Ba-zA-Z0-9_%5Cx7f-%5Cxff%5D%2A%24%2F'
         21        SEND_VAR                                                 !3
         22        DO_ICALL                                         $14     
         23        BOOL_NOT                                         ~15     $14
         24      > JMPZ                                                     ~15, ->31
   27    25    >   NEW                                              $16     'Exception'
         26        CONCAT                                           ~17     'Invalid+key+name%3A+%22', !3
         27        CONCAT                                           ~18     ~17, '%22'
         28        SEND_VAL_EX                                              ~18
         29        DO_FCALL                                      0          
         30      > THROW                                         0          $16
   25    31    > > JMP                                                      ->17
         32    >   FE_FREE                                                  $11
   29    33    >   FETCH_OBJ_W                                      $20     '_assigns'
         34        ASSIGN_DIM                                               $20, !0
         35        OP_DATA                                                  !1
   30    36      > RETURN                                                   null

End of function assign

Function _gettoken:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 19
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bkpBp
function name:  _getToken
number of ops:  31
compiled vars:  !0 = $string, !1 = $sign, !2 = $pos, !3 = $token, !4 = $position
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
   34     4        INIT_FCALL                                               'strpos'
          5        SEND_VAR                                                 !0
          6        SEND_VAR                                                 !1
          7        SEND_VAR                                                 !2
          8        DO_ICALL                                         $5      
          9        ASSIGN                                                   !4, $5
   35    10        TYPE_CHECK                                    4          !4
         11      > JMPZ                                                     ~7, ->19
   37    12    >   INIT_FCALL                                               'substr'
         13        SEND_VAR                                                 !0
         14        SEND_VAR                                                 !2
         15        DO_ICALL                                         $8      
         16        ASSIGN                                                   !3, $8
   38    17        ASSIGN                                                   !2, <false>
         18      > JMP                                                      ->29
   42    19    >   INIT_FCALL                                               'substr'
         20        SEND_VAR                                                 !0
         21        SEND_VAR                                                 !2
         22        SUB                                              ~11     !4, !2
         23        SEND_VAL                                                 ~11
         24        DO_ICALL                                         $12     
         25        ASSIGN                                                   !3, $12
   43    26        STRLEN                                           ~14     !1
         27        ADD                                              ~15     !4, ~14
         28        ASSIGN                                                   !2, ~15
   45    29    > > RETURN                                                   !2
   46    30*     > RETURN                                                   null

End of function _gettoken

Function _execute:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bkpBp
function name:  _execute
number of ops:  20
compiled vars:  !0 = $prepared_code, !1 = $assigns, !2 = $__prepared_code, !3 = $__assigns, !4 = $content
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   55     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   57     2        ASSIGN                                                   !2, !0
   58     3        UNSET_CV                                                 !0
   60     4        ASSIGN                                                   !3, !1
   61     5        UNSET_CV                                                 !1
   63     6        INIT_FCALL                                               'extract'
          7        SEND_REF                                                 !3
          8        DO_ICALL                                                 
   64     9        UNSET_CV                                                 !3
   66    10        INIT_FCALL                                               'ob_start'
         11        DO_ICALL                                                 
   67    12        INCLUDE_OR_EVAL                                          !2, EVAL
   68    13        INIT_FCALL                                               'ob_get_contents'
         14        DO_ICALL                                         $10     
         15        ASSIGN                                                   !4, $10
   69    16        INIT_FCALL                                               'ob_end_clean'
         17        DO_ICALL                                                 
   70    18      > RETURN                                                   !4
   71    19*     > RETURN                                                   null

End of function _execute

Function parse:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
2 jumps found. (Code = 44) Position 1 = 75, Position 2 = 3
Branch analysis from position: 75
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 73
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 28
Branch analysis from position: 24
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 28
2 jumps found. (Code = 44) Position 1 = 33, Position 2 = 50
Branch analysis from position: 33
2 jumps found. (Code = 44) Position 1 = 39, Position 2 = 55
Branch analysis from position: 39
2 jumps found. (Code = 44) Position 1 = 44, Position 2 = 62
Branch analysis from position: 44
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 64
Branch analysis from position: 49
1 jumps found. (Code = 42) Position 1 = 66
Branch analysis from position: 66
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 64
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
Branch analysis from position: 62
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
Branch analysis from position: 55
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
Branch analysis from position: 50
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
Branch analysis from position: 73
filename:       /in/bkpBp
function name:  parse
number of ops:  83
compiled vars:  !0 = $prepared_code, !1 = $pos, !2 = $token, !3 = $match
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   75     0  E >   ASSIGN                                                   !0, '%3F%3E'
   76     1        ASSIGN                                                   !1, 0
   77     2      > JMP                                                      ->73
   79     3    >   INIT_METHOD_CALL                                         '_getToken'
          4        FETCH_OBJ_R                                      ~6      '_template'
          5        SEND_VAL                                                 ~6
          6        SEND_VAL                                                 '%7B'
          7        SEND_VAR                                                 !1
          8        SEND_REF                                                 !2
          9        DO_FCALL                                      0  $7      
         10        ASSIGN                                                   !1, $7
   80    11        ASSIGN_OP                                     8          !0, !2
   81    12        TYPE_CHECK                                  1018          !1
         13      > JMPZ                                                     ~10, ->73
   83    14    >   INIT_METHOD_CALL                                         '_getToken'
         15        FETCH_OBJ_R                                      ~11     '_template'
         16        SEND_VAL                                                 ~11
         17        SEND_VAL                                                 '%7D'
         18        SEND_VAR                                                 !1
         19        SEND_REF                                                 !2
         20        DO_FCALL                                      0  $12     
         21        ASSIGN                                                   !1, $12
   84    22        TYPE_CHECK                                    4          !1
         23      > JMPZ                                                     ~14, ->28
   85    24    >   NEW                                              $15     'Exception'
         25        SEND_VAL_EX                                              'Template+syntax+error.+Expected+%22%7D%22'
         26        DO_FCALL                                      0          
         27      > THROW                                         0          $15
   89    28    >   INIT_FCALL                                               'preg_match'
         29        SEND_VAL                                                 '%2F%5E%28%3F%21__%29%5Ba-zA-Z0-9_%5Cx7f-%5Cxff%5D%2A%24%2F'
         30        SEND_VAR                                                 !2
         31        DO_ICALL                                         $18     
         32      > JMPNZ                                                    $18, ->50
   93    33    >   INIT_FCALL                                               'preg_match'
         34        SEND_VAL                                                 '%2F%5Eloop+%2B%28%28%3F%21__%29%5Ba-zA-Z0-9_%5Cx7f-%5Cxff%5D%2A%29%24%2F'
         35        SEND_VAR                                                 !2
         36        SEND_REF                                                 !3
         37        DO_ICALL                                         $19     
         38      > JMPNZ                                                    $19, ->55
   98    39    >   INIT_FCALL                                               'preg_match'
         40        SEND_VAL                                                 '%2F%5Eend+%2Bloop%24%2F'
         41        SEND_VAR                                                 !2
         42        DO_ICALL                                         $20     
         43      > JMPNZ                                                    $20, ->62
  102    44    >   INIT_FCALL                                               'preg_match'
         45        SEND_VAL                                                 '%2F%5E%5C%25index%5C%25%24%2F'
         46        SEND_VAR                                                 !2
         47        DO_ICALL                                         $21     
         48      > JMPNZ                                                    $21, ->64
         49    > > JMP                                                      ->66
   90    50    >   CONCAT                                           ~22     '%3C%3Fphp+echo+%24%7B%27', !2
         51        CONCAT                                           ~23     ~22, '%27%7D+%3F'
         52        CONCAT                                           ~24     ~23, '%3E'
         53        ASSIGN_OP                                     8          !0, ~24
   91    54      > JMP                                                      ->73
   94    55    >   FETCH_DIM_R                                      ~26     !3, 1
         56        CONCAT                                           ~27     '%3C%3Fphp+foreach+%28%24%7B%27', ~26
         57        CONCAT                                           ~28     ~27, '%27%7D+as+%24__index+%3D%3E+%24__tmp%29'
   95    58        CONCAT                                           ~29     ~28, '%7Bextract%28%24__tmp%29%3B+unset%28%24__tmp%29%3B+%3F'
         59        CONCAT                                           ~30     ~29, '%3E'
         60        ASSIGN_OP                                     8          !0, ~30
   96    61      > JMP                                                      ->73
   99    62    >   ASSIGN_OP                                     8          !0, '%3C%3Fphp+%7D+%3F%3E'
  100    63      > JMP                                                      ->73
  103    64    >   ASSIGN_OP                                     8          !0, '%3C%3Fphp+echo+%24__index+%3F%3E'
  104    65      > JMP                                                      ->73
  107    66    >   NEW                                              $34     'Exception'
         67        CONCAT                                           ~35     'Template+syntax+error.+Unexpected+token+%22', !2
         68        CONCAT                                           ~36     ~35, '%22'
         69        SEND_VAL_EX                                              ~36
         70        DO_FCALL                                      0          
         71      > THROW                                         0          $34
  108    72*       JMP                                                      ->73
   77    73    >   TYPE_CHECK                                  1018          !1
         74      > JMPNZ                                                    ~38, ->3
  112    75    >   ECHO                                                     !0
  113    76        INIT_STATIC_METHOD_CALL                                  '_execute'
         77        SEND_VAR                                                 !0
         78        FETCH_OBJ_R                                      ~39     '_assigns'
         79        SEND_VAL                                                 ~39
         80        DO_FCALL                                      0  $40     
         81      > RETURN                                                   $40
  114    82*     > RETURN                                                   null

End of function parse

End of class Template.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
172.17 ms | 1416 KiB | 27 Q