3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Parses routes of the following form: * * "/user/{name}/{id:[0-9]+}" */ class Std { const VARIABLE_REGEX = <<<'REGEX' ~\{ \s* ([a-zA-Z][a-zA-Z0-9_]*) \s* (?: : \s* ([^{}]*(?:\{(?-1)\}[^{}]*)*) )? \}~x REGEX; const DEFAULT_DISPATCH_REGEX = '[^/]+'; public function parse($route) { if (!preg_match_all( self::VARIABLE_REGEX, $route, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER )) { return [$route]; } $offset = 0; $routeData = []; foreach ($matches as $set) { if ($set[0][1] > $offset) { $routeData[] = substr($route, $offset, $set[0][1] - $offset); } $routeData[] = [ $set[1][0], isset($set[2]) ? trim($set[2][0]) : self::DEFAULT_DISPATCH_REGEX ]; $offset = $set[0][1] + strlen($set[0][0]); } if ($offset != strlen($route)) { $routeData[] = substr($route, $offset); } return $routeData; } } $Parser = new Std; var_dump( $Parser->parse("/user/{name}/{id:[0-9]+}/franchise") );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pYdg9
function name:  (null)
number of ops:  10
compiled vars:  !0 = $Parser
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   NEW                                              $1      'Std'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   49     3        INIT_FCALL                                               'var_dump'
          4        INIT_METHOD_CALL                                         !0, 'parse'
          5        SEND_VAL_EX                                              '%2Fuser%2F%7Bname%7D%2F%7Bid%3A%5B0-9%5D%2B%7D%2Ffranchise'
          6        DO_FCALL                                      0  $4      
          7        SEND_VAR                                                 $4
          8        DO_ICALL                                                 
          9      > RETURN                                                   1

Class Std:
Function parse:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 11
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 77) Position 1 = 14, Position 2 = 53
Branch analysis from position: 14
2 jumps found. (Code = 78) Position 1 = 15, Position 2 = 53
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 29
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 41
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 42
Branch analysis from position: 42
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 29
Branch analysis from position: 53
2 jumps found. (Code = 43) Position 1 = 57, Position 2 = 63
Branch analysis from position: 57
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 63
Branch analysis from position: 53
filename:       /in/pYdg9
function name:  parse
number of ops:  65
compiled vars:  !0 = $route, !1 = $matches, !2 = $offset, !3 = $routeData, !4 = $set
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
   20     1        INIT_FCALL                                               'preg_match_all'
   21     2        SEND_VAL                                                 '%7E%5C%7B%0A++++%5Cs%2A+%28%5Ba-zA-Z%5D%5Ba-zA-Z0-9_%5D%2A%29+%5Cs%2A%0A++++%28%3F%3A%0A++++++++%3A+%5Cs%2A+%28%5B%5E%7B%7D%5D%2A%28%3F%3A%5C%7B%28%3F-1%29%5C%7D%5B%5E%7B%7D%5D%2A%29%2A%29%0A++++%29%3F%0A%5C%7D%7Ex'
          3        SEND_VAR                                                 !0
          4        SEND_REF                                                 !1
          5        SEND_VAL                                                 258
          6        DO_ICALL                                         $5      
          7        BOOL_NOT                                         ~6      $5
          8      > JMPZ                                                     ~6, ->11
   23     9    >   INIT_ARRAY                                       ~7      !0
         10      > RETURN                                                   ~7
   26    11    >   ASSIGN                                                   !2, 0
   27    12        ASSIGN                                                   !3, <array>
   28    13      > FE_RESET_R                                       $10     !1, ->53
         14    > > FE_FETCH_R                                               $10, !4, ->53
   29    15    >   FETCH_DIM_R                                      ~11     !4, 0
         16        FETCH_DIM_R                                      ~12     ~11, 1
         17        IS_SMALLER                                               !2, ~12
         18      > JMPZ                                                     ~13, ->29
   30    19    >   INIT_FCALL                                               'substr'
         20        SEND_VAR                                                 !0
         21        SEND_VAR                                                 !2
         22        FETCH_DIM_R                                      ~15     !4, 0
         23        FETCH_DIM_R                                      ~16     ~15, 1
         24        SUB                                              ~17     ~16, !2
         25        SEND_VAL                                                 ~17
         26        DO_ICALL                                         $18     
         27        ASSIGN_DIM                                               !3
         28        OP_DATA                                                  $18
   33    29    >   FETCH_DIM_R                                      ~20     !4, 1
         30        FETCH_DIM_R                                      ~21     ~20, 0
         31        INIT_ARRAY                                       ~22     ~21
   34    32        ISSET_ISEMPTY_DIM_OBJ                         0          !4, 2
         33      > JMPZ                                                     ~23, ->41
         34    >   INIT_FCALL                                               'trim'
         35        FETCH_DIM_R                                      ~24     !4, 2
         36        FETCH_DIM_R                                      ~25     ~24, 0
         37        SEND_VAL                                                 ~25
         38        DO_ICALL                                         $26     
         39        QM_ASSIGN                                        ~27     $26
         40      > JMP                                                      ->42
   33    41    >   QM_ASSIGN                                        ~27     '%5B%5E%2F%5D%2B'
         42    >   ADD_ARRAY_ELEMENT                                ~22     ~27
   32    43        ASSIGN_DIM                                               !3
   33    44        OP_DATA                                                  ~22
   36    45        FETCH_DIM_R                                      ~28     !4, 0
         46        FETCH_DIM_R                                      ~29     ~28, 1
         47        FETCH_DIM_R                                      ~30     !4, 0
         48        FETCH_DIM_R                                      ~31     ~30, 0
         49        STRLEN                                           ~32     ~31
         50        ADD                                              ~33     ~29, ~32
         51        ASSIGN                                                   !2, ~33
   28    52      > JMP                                                      ->14
         53    >   FE_FREE                                                  $10
   39    54        STRLEN                                           ~35     !0
         55        IS_NOT_EQUAL                                             !2, ~35
         56      > JMPZ                                                     ~36, ->63
   40    57    >   INIT_FCALL                                               'substr'
         58        SEND_VAR                                                 !0
         59        SEND_VAR                                                 !2
         60        DO_ICALL                                         $38     
         61        ASSIGN_DIM                                               !3
         62        OP_DATA                                                  $38
   43    63    > > RETURN                                                   !3
   44    64*     > RETURN                                                   null

End of function parse

End of class Std.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
191.94 ms | 1396 KiB | 21 Q