3v4l.org

run code in 300+ PHP versions simultaneously
<?php function renderBlock(array $m) { $callable = "build$m[1]"; return function_exists($callable) ? $callable($m[2] ?? '') : $m[0]; } function buildPiechart(string $payloadString) { $values = [ 'angle' => 0, 'colorset' => 'red', 'stroke' => 1, 'value' => 1 ]; $rules = [ 'angle' => '/\d+/', 'colorset' => '/reds|yellows|blues/i', 'stroke' => '/\d+/', 'value' => '/\d+/', ]; $attributes = preg_split( '/\h+/u', $payloadString, 0, PREG_SPLIT_NO_EMPTY ); foreach ($attributes as $pair) { [$key, $value] = explode(':', $pair, 2); if ( key_exists($key, $values) && preg_match($rules[$key], $value, $m) ) { $values[$key] = $m[0]; } } return sprintf( '<pie a="%s" c="%s" s="%s" v="%s">', ...array_values($values) ); } $text = 'here is a block to be replaced [block:piechart value:25 angle:0] [block] and [block:notcoded attr:val] another one [block:piechart colorset:reds value:20]'; echo preg_replace_callback( '/\[block:([a-z]+)\h*([^\]\r\n]*)\]/u', 'renderBlock', $text );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/f8kAs
function name:  (null)
number of ops:  8
compiled vars:  !0 = $text
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   ASSIGN                                                   !0, 'here+is+a+block+to+be+replaced+%5Bblock%3Apiechart+value%3A25+++angle%3A0%5D++%5Bblock%5D+and+%5Bblock%3Anotcoded+attr%3Aval%5D+another+one+%5Bblock%3Apiechart+colorset%3Areds+value%3A20%5D'
   46     1        INIT_FCALL                                               'preg_replace_callback'
   47     2        SEND_VAL                                                 '%2F%5C%5Bblock%3A%28%5Ba-z%5D%2B%29%5Ch%2A%28%5B%5E%5C%5D%5Cr%5Cn%5D%2A%29%5C%5D%2Fu'
   48     3        SEND_VAL                                                 'renderBlock'
   49     4        SEND_VAR                                                 !0
   46     5        DO_ICALL                                         $2      
   49     6        ECHO                                                     $2
   50     7      > RETURN                                                   1

Function renderblock:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 17
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/f8kAs
function name:  renderBlock
number of ops:  21
compiled vars:  !0 = $m, !1 = $callable
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        NOP                                                      
          2        FETCH_DIM_R                                      ~2      !0, 1
          3        FAST_CONCAT                                      ~3      'build', ~2
          4        ASSIGN                                                   !1, ~3
    5     5        INIT_FCALL                                               'function_exists'
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $5      
          8      > JMPZ                                                     $5, ->17
    6     9    >   INIT_DYNAMIC_CALL                                        !1
         10        FETCH_DIM_IS                                     ~6      !0, 2
         11        COALESCE                                         ~7      ~6
         12        QM_ASSIGN                                        ~7      ''
         13        SEND_VAL_EX                                              ~7
         14        DO_FCALL                                      0  $8      
         15        QM_ASSIGN                                        ~9      $8
         16      > JMP                                                      ->19
    7    17    >   FETCH_DIM_R                                      ~10     !0, 0
         18        QM_ASSIGN                                        ~9      ~10
         19    > > RETURN                                                   ~9
    8    20*     > RETURN                                                   null

End of function renderblock

Function buildpiechart:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 39
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 39
Branch analysis from position: 12
2 jumps found. (Code = 46) Position 1 = 27, Position 2 = 34
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 38
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 38
Branch analysis from position: 34
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 39
filename:       /in/f8kAs
function name:  buildPiechart
number of ops:  50
compiled vars:  !0 = $payloadString, !1 = $values, !2 = $rules, !3 = $attributes, !4 = $pair, !5 = $key, !6 = $value, !7 = $m
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
   11     1        ASSIGN                                                   !1, <array>
   17     2        ASSIGN                                                   !2, <array>
   23     3        INIT_FCALL                                               'preg_split'
   24     4        SEND_VAL                                                 '%2F%5Ch%2B%2Fu'
   25     5        SEND_VAR                                                 !0
   26     6        SEND_VAL                                                 0
   27     7        SEND_VAL                                                 1
   23     8        DO_ICALL                                         $10     
          9        ASSIGN                                                   !3, $10
   29    10      > FE_RESET_R                                       $12     !3, ->39
         11    > > FE_FETCH_R                                               $12, !4, ->39
   30    12    >   INIT_FCALL                                               'explode'
         13        SEND_VAL                                                 '%3A'
         14        SEND_VAR                                                 !4
         15        SEND_VAL                                                 2
         16        DO_ICALL                                         $13     
         17        FETCH_LIST_R                                     $14     $13, 0
         18        ASSIGN                                                   !5, $14
         19        FETCH_LIST_R                                     $16     $13, 1
         20        ASSIGN                                                   !6, $16
         21        FREE                                                     $13
   32    22        INIT_FCALL                                               'key_exists'
         23        SEND_VAR                                                 !5
         24        SEND_VAR                                                 !1
         25        DO_ICALL                                         $18     
         26      > JMPZ_EX                                          ~19     $18, ->34
   33    27    >   INIT_FCALL                                               'preg_match'
         28        FETCH_DIM_R                                      ~20     !2, !5
         29        SEND_VAL                                                 ~20
         30        SEND_VAR                                                 !6
         31        SEND_REF                                                 !7
         32        DO_ICALL                                         $21     
         33        BOOL                                             ~19     $21
         34    > > JMPZ                                                     ~19, ->38
   35    35    >   FETCH_DIM_R                                      ~23     !7, 0
         36        ASSIGN_DIM                                               !1, !5
         37        OP_DATA                                                  ~23
   29    38    > > JMP                                                      ->11
         39    >   FE_FREE                                                  $12
   38    40        INIT_FCALL                                               'sprintf'
   39    41        SEND_VAL                                                 '%3Cpie+a%3D%22%25s%22+c%3D%22%25s%22+s%3D%22%25s%22+v%3D%22%25s%22%3E'
   40    42        INIT_FCALL                                               'array_values'
         43        SEND_VAR                                                 !1
         44        DO_ICALL                                         $24     
         45        SEND_UNPACK                                              $24
         46        CHECK_UNDEF_ARGS                                         
   38    47        DO_ICALL                                         $25     
   40    48      > RETURN                                                   $25
   42    49*     > RETURN                                                   null

End of function buildpiechart

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
141.28 ms | 1016 KiB | 21 Q