3v4l.org

run code in 300+ PHP versions simultaneously
<?php function parse($pattern) { $matches = ''; $variables = array(); $pos = 0; $reg = '#'; //It seems that regex must start and end with a delimiter $nextText = ''; if($pattern == '/') { $reg = '#^[\/]+$#'; return ['variables' => '', 'regex' => $reg]; } //Check if generated regexes are stored, if so it skips the whole process /*if(apc_exists($pattern)) { $cacheI = apc_fetch($pattern); return $cacheI; }*/ //Extracts the variables enclosed in {} preg_match_all('#\{\w+\}#', $pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); //Puts each variable in array //Uses the text before and after to create a regex for the rest of the pattern - $precedingText, $nextText //If no wildcard is detected in the path it splits it into segments and compiles are regex foreach ($matches as $match) { $varName = substr($match[0][0], 1, -1); $precedingText = substr($pattern, $pos, $match[0][1] - $pos); $pos = $match[0][1] + strlen($match[0][0]); $nxt = $pos - strlen($pattern); if($nxt == 0) $nxt = strlen($pattern); $nextText = substr($pattern, $nxt); $precSegments = explode('/', $precedingText); $precSegments = array_splice($precSegments, 1); //Pulls a regex from the preeceding segment, each variable segment is replaced with '\/[a-zA-Z0-9]+' if(strlen($precedingText) > 1) { foreach($precSegments as $key => $value) { $reg .= '\/'; $reg .= $value; } $reg .= '[a-zA-Z0-9]+'; } else { $reg .= '\/[a-zA-Z0-9]+'; } $nextText = str_replace('/', '\/', $nextText); if(is_numeric($varName)) { throw new \Exception('Argument cannot be a number'); } if (in_array($varName, $variables)) { throw new \Exception(sprintf('More then one occurrence of variable name "%s".', $varName)); } $variables[] = $varName; } //If no variable names, wildcards are found in pattern : /hello/static/path it will replace it with \/hello\/static\/path if(count($matches) < 1 && $pattern != '/') { $reg .= str_replace('/', '\/', $pattern); } $reg = $reg . $nextText; $reg .= '#'; //apc_store($pattern, ['variables' => $variables, 'regex' => $reg]); return ['variables' => $variables, 'regex' => $reg]; } $data = parse('hi/{p1}/cicki/{p2}'); echo 'parse: '.var_export($data, 1);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7as7Z
function name:  (null)
number of ops:  11
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   84     0  E >   INIT_FCALL                                               'parse'
          1        SEND_VAL                                                 'hi%2F%7Bp1%7D%2Fcicki%2F%7Bp2%7D'
          2        DO_FCALL                                      0  $1      
          3        ASSIGN                                                   !0, $1
   85     4        INIT_FCALL                                               'var_export'
          5        SEND_VAR                                                 !0
          6        SEND_VAL                                                 1
          7        DO_ICALL                                         $3      
          8        CONCAT                                           ~4      'parse%3A+', $3
          9        ECHO                                                     ~4
         10      > RETURN                                                   1

Function parse:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 12
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 77) Position 1 = 19, Position 2 = 109
Branch analysis from position: 19
2 jumps found. (Code = 78) Position 1 = 20, Position 2 = 109
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 49, Position 2 = 51
Branch analysis from position: 49
2 jumps found. (Code = 43) Position 1 = 69, Position 2 = 78
Branch analysis from position: 69
2 jumps found. (Code = 77) Position 1 = 70, Position 2 = 75
Branch analysis from position: 70
2 jumps found. (Code = 78) Position 1 = 71, Position 2 = 75
Branch analysis from position: 71
1 jumps found. (Code = 42) Position 1 = 70
Branch analysis from position: 70
Branch analysis from position: 75
1 jumps found. (Code = 42) Position 1 = 79
Branch analysis from position: 79
2 jumps found. (Code = 43) Position 1 = 89, Position 2 = 93
Branch analysis from position: 89
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 93
2 jumps found. (Code = 43) Position 1 = 98, Position 2 = 106
Branch analysis from position: 98
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 106
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
Branch analysis from position: 75
Branch analysis from position: 78
2 jumps found. (Code = 43) Position 1 = 89, Position 2 = 93
Branch analysis from position: 89
Branch analysis from position: 93
Branch analysis from position: 51
Branch analysis from position: 109
2 jumps found. (Code = 46) Position 1 = 113, Position 2 = 115
Branch analysis from position: 113
2 jumps found. (Code = 43) Position 1 = 116, Position 2 = 122
Branch analysis from position: 116
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 122
Branch analysis from position: 115
Branch analysis from position: 109
filename:       /in/7as7Z
function name:  parse
number of ops:  129
compiled vars:  !0 = $pattern, !1 = $matches, !2 = $variables, !3 = $pos, !4 = $reg, !5 = $nextText, !6 = $match, !7 = $varName, !8 = $precedingText, !9 = $nxt, !10 = $precSegments, !11 = $value, !12 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
    4     1        ASSIGN                                                   !1, ''
    5     2        ASSIGN                                                   !2, <array>
    6     3        ASSIGN                                                   !3, 0
    7     4        ASSIGN                                                   !4, '%23'
    8     5        ASSIGN                                                   !5, ''
   10     6        IS_EQUAL                                                 !0, '%2F'
          7      > JMPZ                                                     ~18, ->12
   12     8    >   ASSIGN                                                   !4, '%23%5E%5B%5C%2F%5D%2B%24%23'
   14     9        INIT_ARRAY                                       ~20     '', 'variables'
         10        ADD_ARRAY_ELEMENT                                ~20     !4, 'regex'
         11      > RETURN                                                   ~20
   25    12    >   INIT_FCALL                                               'preg_match_all'
         13        SEND_VAL                                                 '%23%5C%7B%5Cw%2B%5C%7D%23'
         14        SEND_VAR                                                 !0
         15        SEND_REF                                                 !1
         16        SEND_VAL                                                 258
         17        DO_ICALL                                                 
   30    18      > FE_RESET_R                                       $22     !1, ->109
         19    > > FE_FETCH_R                                               $22, !6, ->109
   32    20    >   INIT_FCALL                                               'substr'
         21        FETCH_DIM_R                                      ~23     !6, 0
         22        FETCH_DIM_R                                      ~24     ~23, 0
         23        SEND_VAL                                                 ~24
         24        SEND_VAL                                                 1
         25        SEND_VAL                                                 -1
         26        DO_ICALL                                         $25     
         27        ASSIGN                                                   !7, $25
   33    28        INIT_FCALL                                               'substr'
         29        SEND_VAR                                                 !0
         30        SEND_VAR                                                 !3
         31        FETCH_DIM_R                                      ~27     !6, 0
         32        FETCH_DIM_R                                      ~28     ~27, 1
         33        SUB                                              ~29     ~28, !3
         34        SEND_VAL                                                 ~29
         35        DO_ICALL                                         $30     
         36        ASSIGN                                                   !8, $30
   34    37        FETCH_DIM_R                                      ~32     !6, 0
         38        FETCH_DIM_R                                      ~33     ~32, 1
         39        FETCH_DIM_R                                      ~34     !6, 0
         40        FETCH_DIM_R                                      ~35     ~34, 0
         41        STRLEN                                           ~36     ~35
         42        ADD                                              ~37     ~33, ~36
         43        ASSIGN                                                   !3, ~37
   35    44        STRLEN                                           ~39     !0
         45        SUB                                              ~40     !3, ~39
         46        ASSIGN                                                   !9, ~40
   36    47        IS_EQUAL                                                 !9, 0
         48      > JMPZ                                                     ~42, ->51
         49    >   STRLEN                                           ~43     !0
         50        ASSIGN                                                   !9, ~43
   38    51    >   INIT_FCALL                                               'substr'
         52        SEND_VAR                                                 !0
         53        SEND_VAR                                                 !9
         54        DO_ICALL                                         $45     
         55        ASSIGN                                                   !5, $45
   40    56        INIT_FCALL                                               'explode'
         57        SEND_VAL                                                 '%2F'
         58        SEND_VAR                                                 !8
         59        DO_ICALL                                         $47     
         60        ASSIGN                                                   !10, $47
   41    61        INIT_FCALL                                               'array_splice'
         62        SEND_REF                                                 !10
         63        SEND_VAL                                                 1
         64        DO_ICALL                                         $49     
         65        ASSIGN                                                   !10, $49
   44    66        STRLEN                                           ~51     !8
         67        IS_SMALLER                                               1, ~51
         68      > JMPZ                                                     ~52, ->78
   46    69    > > FE_RESET_R                                       $53     !10, ->75
         70    > > FE_FETCH_R                                       ~54     $53, !11, ->75
         71    >   ASSIGN                                                   !12, ~54
   47    72        ASSIGN_OP                                     8          !4, '%5C%2F'
   48    73        ASSIGN_OP                                     8          !4, !11
   46    74      > JMP                                                      ->70
         75    >   FE_FREE                                                  $53
   51    76        ASSIGN_OP                                     8          !4, '%5Ba-zA-Z0-9%5D%2B'
   44    77      > JMP                                                      ->79
   55    78    >   ASSIGN_OP                                     8          !4, '%5C%2F%5Ba-zA-Z0-9%5D%2B'
   58    79    >   INIT_FCALL                                               'str_replace'
         80        SEND_VAL                                                 '%2F'
         81        SEND_VAL                                                 '%5C%2F'
         82        SEND_VAR                                                 !5
         83        DO_ICALL                                         $60     
         84        ASSIGN                                                   !5, $60
   60    85        INIT_FCALL                                               'is_numeric'
         86        SEND_VAR                                                 !7
         87        DO_ICALL                                         $62     
         88      > JMPZ                                                     $62, ->93
   61    89    >   NEW                                              $63     'Exception'
         90        SEND_VAL_EX                                              'Argument+cannot+be+a+number'
         91        DO_FCALL                                      0          
         92      > THROW                                         0          $63
   64    93    >   INIT_FCALL                                               'in_array'
         94        SEND_VAR                                                 !7
         95        SEND_VAR                                                 !2
         96        DO_ICALL                                         $65     
         97      > JMPZ                                                     $65, ->106
   65    98    >   NEW                                              $66     'Exception'
         99        INIT_FCALL                                               'sprintf'
        100        SEND_VAL                                                 'More+then+one+occurrence+of+variable+name+%22%25s%22.'
        101        SEND_VAR                                                 !7
        102        DO_ICALL                                         $67     
        103        SEND_VAR_NO_REF_EX                                       $67
        104        DO_FCALL                                      0          
        105      > THROW                                         0          $66
   68   106    >   ASSIGN_DIM                                               !2
        107        OP_DATA                                                  !7
   30   108      > JMP                                                      ->19
        109    >   FE_FREE                                                  $22
   72   110        COUNT                                            ~70     !1
        111        IS_SMALLER                                       ~71     ~70, 1
        112      > JMPZ_EX                                          ~71     ~71, ->115
        113    >   IS_NOT_EQUAL                                     ~72     !0, '%2F'
        114        BOOL                                             ~71     ~72
        115    > > JMPZ                                                     ~71, ->122
   74   116    >   INIT_FCALL                                               'str_replace'
        117        SEND_VAL                                                 '%2F'
        118        SEND_VAL                                                 '%5C%2F'
        119        SEND_VAR                                                 !0
        120        DO_ICALL                                         $73     
        121        ASSIGN_OP                                     8          !4, $73
   77   122    >   CONCAT                                           ~75     !4, !5
        123        ASSIGN                                                   !4, ~75
   78   124        ASSIGN_OP                                     8          !4, '%23'
   82   125        INIT_ARRAY                                       ~78     !2, 'variables'
        126        ADD_ARRAY_ELEMENT                                ~78     !4, 'regex'
        127      > RETURN                                                   ~78
   83   128*     > RETURN                                                   null

End of function parse

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
149.2 ms | 1022 KiB | 23 Q