3v4l.org

run code in 300+ PHP versions simultaneously
<?php foreach (cartesian(expand_pattern('a..z0..9'), 2) as $option) { echo "$option\n"; } function expand_pattern($pattern) { $bias = 0; $flags = PREG_SET_ORDER | PREG_OFFSET_CAPTURE; preg_match_all('/(.)\.\.(.)/', $pattern, $matches, $flags); foreach ($matches as $match) { $range = implode('', range($match[1][0], $match[2][0])); $pattern = substr_replace( $pattern, $range, $bias + $match[1][1], $match[2][1] - $match[1][1] + 1); $bias += strlen($range) - 4; } return $pattern; } function cartesian($pattern, $length) { $choices = strlen($pattern); $indexes = array_fill(0, $length, 0); $resets = 0; while ($resets != $length) { $result = ''; for ($i = 0; $i < $length; ++$i) { $result .= $pattern[$indexes[$i]]; } yield $result; $resets = 0; for ($i = $length - 1; $i >= 0 && ++$indexes[$i] == $choices; --$i) { $indexes[$i] = 0; ++$resets; } } }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 13
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 13
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/F8dk0
function name:  (null)
number of ops:  15
compiled vars:  !0 = $option
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   INIT_FCALL_BY_NAME                                       'cartesian'
          1        INIT_FCALL_BY_NAME                                       'expand_pattern'
          2        SEND_VAL_EX                                              'a..z0..9'
          3        DO_FCALL                                      0  $1      
          4        SEND_VAR_NO_REF_EX                                       $1
          5        SEND_VAL_EX                                              2
          6        DO_FCALL                                      0  $2      
          7      > FE_RESET_R                                       $3      $2, ->13
          8    > > FE_FETCH_R                                               $3, !0, ->13
    5     9    >   NOP                                                      
         10        FAST_CONCAT                                      ~4      !0, '%0A'
         11        ECHO                                                     ~4
    4    12      > JMP                                                      ->8
         13    >   FE_FREE                                                  $3
   44    14      > RETURN                                                   1

Function expand_pattern:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 10, Position 2 = 44
Branch analysis from position: 10
2 jumps found. (Code = 78) Position 1 = 11, Position 2 = 44
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 44
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 44
filename:       /in/F8dk0
function name:  expand_pattern
number of ops:  47
compiled vars:  !0 = $pattern, !1 = $bias, !2 = $flags, !3 = $matches, !4 = $match, !5 = $range
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
   10     1        ASSIGN                                                   !1, 0
   11     2        ASSIGN                                                   !2, 258
   12     3        INIT_FCALL                                               'preg_match_all'
          4        SEND_VAL                                                 '%2F%28.%29%5C.%5C.%28.%29%2F'
          5        SEND_VAR                                                 !0
          6        SEND_REF                                                 !3
          7        SEND_VAR                                                 !2
          8        DO_ICALL                                                 
   13     9      > FE_RESET_R                                       $9      !3, ->44
         10    > > FE_FETCH_R                                               $9, !4, ->44
   14    11    >   INIT_FCALL                                               'implode'
         12        SEND_VAL                                                 ''
         13        INIT_FCALL                                               'range'
         14        FETCH_DIM_R                                      ~10     !4, 1
         15        FETCH_DIM_R                                      ~11     ~10, 0
         16        SEND_VAL                                                 ~11
         17        FETCH_DIM_R                                      ~12     !4, 2
         18        FETCH_DIM_R                                      ~13     ~12, 0
         19        SEND_VAL                                                 ~13
         20        DO_ICALL                                         $14     
         21        SEND_VAR                                                 $14
         22        DO_ICALL                                         $15     
         23        ASSIGN                                                   !5, $15
   15    24        INIT_FCALL                                               'substr_replace'
   16    25        SEND_VAR                                                 !0
   17    26        SEND_VAR                                                 !5
   18    27        FETCH_DIM_R                                      ~17     !4, 1
         28        FETCH_DIM_R                                      ~18     ~17, 1
         29        ADD                                              ~19     !1, ~18
         30        SEND_VAL                                                 ~19
   19    31        FETCH_DIM_R                                      ~20     !4, 2
         32        FETCH_DIM_R                                      ~21     ~20, 1
         33        FETCH_DIM_R                                      ~22     !4, 1
         34        FETCH_DIM_R                                      ~23     ~22, 1
         35        SUB                                              ~24     ~21, ~23
         36        ADD                                              ~25     ~24, 1
         37        SEND_VAL                                                 ~25
         38        DO_ICALL                                         $26     
   15    39        ASSIGN                                                   !0, $26
   20    40        STRLEN                                           ~28     !5
         41        SUB                                              ~29     ~28, 4
         42        ASSIGN_OP                                     1          !1, ~29
   13    43      > JMP                                                      ->10
         44    >   FE_FREE                                                  $9
   23    45      > RETURN                                                   !0
   24    46*     > RETURN                                                   null

End of function expand_pattern

Function cartesian:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
2 jumps found. (Code = 44) Position 1 = 40, Position 2 = 13
Branch analysis from position: 40
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 22, Position 2 = 16
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
2 jumps found. (Code = 46) Position 1 = 33, Position 2 = 37
Branch analysis from position: 33
2 jumps found. (Code = 44) Position 1 = 38, Position 2 = 27
Branch analysis from position: 38
Branch analysis from position: 27
2 jumps found. (Code = 46) Position 1 = 33, Position 2 = 37
Branch analysis from position: 33
Branch analysis from position: 37
Branch analysis from position: 37
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 22, Position 2 = 16
Branch analysis from position: 22
Branch analysis from position: 16
filename:       /in/F8dk0
function name:  cartesian
number of ops:  41
compiled vars:  !0 = $pattern, !1 = $length, !2 = $choices, !3 = $indexes, !4 = $resets, !5 = $result, !6 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        GENERATOR_CREATE                                         
   27     3        STRLEN                                           ~7      !0
          4        ASSIGN                                                   !2, ~7
   28     5        INIT_FCALL                                               'array_fill'
          6        SEND_VAL                                                 0
          7        SEND_VAR                                                 !1
          8        SEND_VAL                                                 0
          9        DO_ICALL                                         $9      
         10        ASSIGN                                                   !3, $9
   29    11        ASSIGN                                                   !4, 0
   31    12      > JMP                                                      ->38
   32    13    >   ASSIGN                                                   !5, ''
   33    14        ASSIGN                                                   !6, 0
         15      > JMP                                                      ->20
   34    16    >   FETCH_DIM_R                                      ~14     !3, !6
         17        FETCH_DIM_R                                      ~15     !0, ~14
         18        ASSIGN_OP                                     8          !5, ~15
   33    19        PRE_INC                                                  !6
         20    >   IS_SMALLER                                               !6, !1
         21      > JMPNZ                                                    ~18, ->16
   36    22    >   YIELD                                                    !5
   38    23        ASSIGN                                                   !4, 0
   39    24        SUB                                              ~21     !1, 1
         25        ASSIGN                                                   !6, ~21
         26      > JMP                                                      ->31
   40    27    >   ASSIGN_DIM                                               !3, !6
         28        OP_DATA                                                  0
   41    29        PRE_INC                                                  !4
   39    30        PRE_DEC                                                  !6
         31    >   IS_SMALLER_OR_EQUAL                              ~26     0, !6
         32      > JMPZ_EX                                          ~26     ~26, ->37
         33    >   FETCH_DIM_RW                                     $27     !3, !6
         34        PRE_INC                                          ~28     $27
         35        IS_EQUAL                                         ~29     !2, ~28
         36        BOOL                                             ~26     ~29
         37    > > JMPNZ                                                    ~26, ->27
   31    38    >   IS_NOT_EQUAL                                             !4, !1
         39      > JMPNZ                                                    ~30, ->13
   44    40    > > GENERATOR_RETURN                                         

End of function cartesian

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
184.05 ms | 1404 KiB | 23 Q