3v4l.org

run code in 300+ PHP versions simultaneously
<?php function extractPatterns(array $rules) : array { $postcodes = []; foreach ($rules as $rule) { if (preg_match('/^([A-Z]{1,2})([0-9]{1,2})-([0-9]{1,2})$/', trim($rule), $matches)) { for ($i = $matches[2]; $i <= $matches[3]; $i++) { $postcodes[] = sprintf('%s%s', $matches[1], $i); } continue; } if (preg_match('([A-Z]{1,2}[0-9]{1,2})', $rule, $matches)) { $postcodes[] = $matches[0]; continue; } if (preg_match('[A-Z]{1,2})', $rule, $matches)) { $postcodes[] = $matches[0]; continue; } } return $postcodes; } $tests = [ [ 'rules' => 'AB41-56, FK19-21, NG12', 'expected' => [ 'AB41', 'AB42', 'AB43', 'AB44', 'AB45', 'AB46', 'AB47', 'AB48', 'AB49', 'AB50', 'AB51', 'AB52', 'AB53', 'AB54', 'AB55', 'AB56', 'FK19', 'FK20', 'FK21', 'NG12' ] ], [ 'rules' => 'HU1, W1-5', 'expected' => [ 'HU1', 'W1', 'W2', 'W3', 'W4', 'W5' ] ] ]; foreach ($tests as $test) { assert(extractPatterns(explode(',', $test['rules'])) == $test['expected']); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 19
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 19
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
filename:       /in/cf8aZ
function name:  (null)
number of ops:  21
compiled vars:  !0 = $tests, !1 = $test
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   ASSIGN                                                   !0, <array>
   67     1      > FE_RESET_R                                       $3      !0, ->19
          2    > > FE_FETCH_R                                               $3, !1, ->19
   68     3    >   ASSERT_CHECK                                             
          4        INIT_FCALL                                               'assert'
          5        INIT_FCALL                                               'extractpatterns'
          6        INIT_FCALL                                               'explode'
          7        SEND_VAL                                                 '%2C'
          8        FETCH_DIM_R                                      ~4      !1, 'rules'
          9        SEND_VAL                                                 ~4
         10        DO_ICALL                                         $5      
         11        SEND_VAR                                                 $5
         12        DO_FCALL                                      0  $6      
         13        FETCH_DIM_R                                      ~7      !1, 'expected'
         14        IS_EQUAL                                         ~8      $6, ~7
         15        SEND_VAL                                                 ~8
         16        SEND_VAL                                                 'assert%28extractPatterns%28explode%28%27%2C%27%2C+%24test%5B%27rules%27%5D%29%29+%3D%3D+%24test%5B%27expected%27%5D%29'
         17        DO_ICALL                                                 
   67    18      > JMP                                                      ->2
         19    >   FE_FREE                                                  $3
   69    20      > RETURN                                                   1

Function extractpatterns:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 50
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 50
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 29
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
2 jumps found. (Code = 44) Position 1 = 28, Position 2 = 16
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 28, Position 2 = 16
Branch analysis from position: 28
Branch analysis from position: 16
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 39
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 39
2 jumps found. (Code = 43) Position 1 = 45, Position 2 = 49
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 49
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 50
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 50
filename:       /in/cf8aZ
function name:  extractPatterns
number of ops:  55
compiled vars:  !0 = $rules, !1 = $postcodes, !2 = $rule, !3 = $matches, !4 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        ASSIGN                                                   !1, <array>
    7     2      > FE_RESET_R                                       $6      !0, ->50
          3    > > FE_FETCH_R                                               $6, !2, ->50
    8     4    >   INIT_FCALL                                               'preg_match'
          5        SEND_VAL                                                 '%2F%5E%28%5BA-Z%5D%7B1%2C2%7D%29%28%5B0-9%5D%7B1%2C2%7D%29-%28%5B0-9%5D%7B1%2C2%7D%29%24%2F'
          6        INIT_FCALL                                               'trim'
          7        SEND_VAR                                                 !2
          8        DO_ICALL                                         $7      
          9        SEND_VAR                                                 $7
         10        SEND_REF                                                 !3
         11        DO_ICALL                                         $8      
         12      > JMPZ                                                     $8, ->29
    9    13    >   FETCH_DIM_R                                      ~9      !3, 2
         14        ASSIGN                                                   !4, ~9
         15      > JMP                                                      ->25
   10    16    >   INIT_FCALL                                               'sprintf'
         17        SEND_VAL                                                 '%25s%25s'
         18        FETCH_DIM_R                                      ~12     !3, 1
         19        SEND_VAL                                                 ~12
         20        SEND_VAR                                                 !4
         21        DO_ICALL                                         $13     
         22        ASSIGN_DIM                                               !1
         23        OP_DATA                                                  $13
    9    24        PRE_INC                                                  !4
         25    >   FETCH_DIM_R                                      ~15     !3, 3
         26        IS_SMALLER_OR_EQUAL                                      !4, ~15
         27      > JMPNZ                                                    ~16, ->16
   12    28    > > JMP                                                      ->3
   14    29    >   INIT_FCALL                                               'preg_match'
         30        SEND_VAL                                                 '%28%5BA-Z%5D%7B1%2C2%7D%5B0-9%5D%7B1%2C2%7D%29'
         31        SEND_VAR                                                 !2
         32        SEND_REF                                                 !3
         33        DO_ICALL                                         $17     
         34      > JMPZ                                                     $17, ->39
   15    35    >   FETCH_DIM_R                                      ~19     !3, 0
         36        ASSIGN_DIM                                               !1
         37        OP_DATA                                                  ~19
   16    38      > JMP                                                      ->3
   18    39    >   INIT_FCALL                                               'preg_match'
         40        SEND_VAL                                                 '%5BA-Z%5D%7B1%2C2%7D%29'
         41        SEND_VAR                                                 !2
         42        SEND_REF                                                 !3
         43        DO_ICALL                                         $20     
         44      > JMPZ                                                     $20, ->49
   19    45    >   FETCH_DIM_R                                      ~22     !3, 0
         46        ASSIGN_DIM                                               !1
         47        OP_DATA                                                  ~22
   20    48      > JMP                                                      ->3
    7    49    > > JMP                                                      ->3
         50    >   FE_FREE                                                  $6
   24    51        VERIFY_RETURN_TYPE                                       !1
         52      > RETURN                                                   !1
   25    53*       VERIFY_RETURN_TYPE                                       
         54*     > RETURN                                                   null

End of function extractpatterns

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
179.26 ms | 1407 KiB | 24 Q