3v4l.org

run code in 500+ PHP versions simultaneously
<?php // If chunk_len is negative, offset starts from the end of the string function str_split_offset( $str, $chunk_len = 1, $offset = 0, $joiner = null ){ $len = strlen( $str ); if( $chunk_len < 0 ){ $chunk_len = -$chunk_len; $offset = $len - $offset; }else if( ! $chunk_len ){ $chunk_len = 1; } $offset = ( $offset % $chunk_len + $chunk_len ) % $chunk_len; if( $offset === 0 ){ $a = str_split( $str, $chunk_len ); }else{ /**/ // You can choose from two workhorses: $str = str_repeat(' ', $chunk_len - $offset ) . $str; $a = str_split( $str, $chunk_len ); $a[0] = substr( $a[0], $chunk_len - $offset ); /*/ $a = array( substr( $str, 0, $offset )); for( ; $offset < $len; $offset += $chunk_len ) $a[] = substr( $str, $offset, $chunk_len ); /**/ } return $joiner !== null ? implode( $joiner, $a ) : $a; } echo "Merry Xmas!\n"; for( $len = 0; $len < 24; ++$len ){ $chunks = str_split_offset( str_repeat('X', $len ), (int)( $len / 3 ), (int)( $len / 2 ), ' '); echo str_pad("/{$chunks}\\", 100, ' ', STR_PAD_BOTH ), "\n"; } echo "\nGroup the digits of powers of two\n"; for( $bits = 0; $bits < 63; ++$bits ){ $chunks = str_split_offset( 1 << $bits, -3, 0, '_'); echo str_pad( $bits, 2, ' ', STR_PAD_LEFT ), str_pad( $chunks, 26, ' ', STR_PAD_LEFT ), "\n"; } $str = '1234567890123'; $chunk_len = 5; echo "\nOffset from left\n"; for( $offset = -$chunk_len * 2; $offset <= $chunk_len * 2; ++$offset ){ $chunks = str_split_offset( $str, $chunk_len, $offset, ' '); echo str_pad( $offset, 3, ' ', STR_PAD_LEFT ), " |{$chunks}|\n"; } echo "\nOffset from right\n"; for( $offset = -$chunk_len * 2; $offset <= $chunk_len * 2; ++$offset ){ $chunks = str_split_offset( $str, -$chunk_len, $offset, ' '); echo str_pad( $offset, 3, ' ', STR_PAD_LEFT ), str_pad("|{$chunks}|\n", 20, ' ', STR_PAD_LEFT ); }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
2 jumps found. (Code = 44) Position 1 = 32, Position 2 = 3
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
2 jumps found. (Code = 44) Position 1 = 61, Position 2 = 35
Branch analysis from position: 61
1 jumps found. (Code = 42) Position 1 = 87
Branch analysis from position: 87
2 jumps found. (Code = 44) Position 1 = 90, Position 2 = 68
Branch analysis from position: 90
1 jumps found. (Code = 42) Position 1 = 121
Branch analysis from position: 121
2 jumps found. (Code = 44) Position 1 = 124, Position 2 = 95
Branch analysis from position: 124
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 95
2 jumps found. (Code = 44) Position 1 = 124, Position 2 = 95
Branch analysis from position: 124
Branch analysis from position: 95
Branch analysis from position: 68
2 jumps found. (Code = 44) Position 1 = 90, Position 2 = 68
Branch analysis from position: 90
Branch analysis from position: 68
Branch analysis from position: 35
2 jumps found. (Code = 44) Position 1 = 61, Position 2 = 35
Branch analysis from position: 61
Branch analysis from position: 35
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 32, Position 2 = 3
Branch analysis from position: 32
Branch analysis from position: 3
filename:       /in/7tf69
function name:  (null)
number of ops:  125
compiled vars:  !0 = $len, !1 = $chunks, !2 = $bits, !3 = $str, !4 = $chunk_len, !5 = $offset
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   34     0  E >   ECHO                                                         'Merry+Xmas%21%0A'
   35     1        ASSIGN                                                       !0, 0
          2      > JMP                                                          ->30
   36     3    >   INIT_FCALL                                                   'str_split_offset'
          4        INIT_FCALL                                                   'str_repeat'
          5        SEND_VAL                                                     'X'
          6        SEND_VAR                                                     !0
          7        DO_ICALL                                             $7      
          8        SEND_VAR                                                     $7
          9        DIV                                                  ~8      !0, 3
         10        CAST                                              4  ~9      ~8
         11        SEND_VAL                                                     ~9
         12        DIV                                                  ~10     !0, 2
         13        CAST                                              4  ~11     ~10
         14        SEND_VAL                                                     ~11
         15        SEND_VAL                                                     '+'
         16        DO_FCALL                                          0  $12     
         17        ASSIGN                                                       !1, $12
   37    18        INIT_FCALL                                                   'str_pad'
         19        ROPE_INIT                                         3  ~15     '%2F'
         20        ROPE_ADD                                          1  ~15     ~15, !1
         21        ROPE_END                                          2  ~14     ~15, '%5C'
         22        SEND_VAL                                                     ~14
         23        SEND_VAL                                                     100
         24        SEND_VAL                                                     '+'
         25        SEND_VAL                                                     2
         26        DO_ICALL                                             $17     
         27        ECHO                                                         $17
         28        ECHO                                                         '%0A'
   35    29        PRE_INC                                                      !0
         30    >   IS_SMALLER                                                   !0, 24
         31      > JMPNZ                                                        ~19, ->3
   40    32    >   ECHO                                                         '%0AGroup+the+digits+of+powers+of+two%0A'
   41    33        ASSIGN                                                       !2, 0
         34      > JMP                                                          ->59
   42    35    >   INIT_FCALL                                                   'str_split_offset'
         36        SL                                                   ~21     1, !2
         37        SEND_VAL                                                     ~21
         38        SEND_VAL                                                     -3
         39        SEND_VAL                                                     0
         40        SEND_VAL                                                     '_'
         41        DO_FCALL                                          0  $22     
         42        ASSIGN                                                       !1, $22
   43    43        INIT_FCALL                                                   'str_pad'
         44        SEND_VAR                                                     !2
         45        SEND_VAL                                                     2
         46        SEND_VAL                                                     '+'
         47        SEND_VAL                                                     0
         48        DO_ICALL                                             $24     
         49        ECHO                                                         $24
         50        INIT_FCALL                                                   'str_pad'
         51        SEND_VAR                                                     !1
         52        SEND_VAL                                                     26
         53        SEND_VAL                                                     '+'
         54        SEND_VAL                                                     0
         55        DO_ICALL                                             $25     
         56        ECHO                                                         $25
         57        ECHO                                                         '%0A'
   41    58        PRE_INC                                                      !2
         59    >   IS_SMALLER                                                   !2, 63
         60      > JMPNZ                                                        ~27, ->35
   46    61    >   ASSIGN                                                       !3, '1234567890123'
   47    62        ASSIGN                                                       !4, 5
   49    63        ECHO                                                         '%0AOffset+from+left%0A'
   50    64        MUL                                                  ~30     !4, -1
         65        MUL                                                  ~31     ~30, 2
         66        ASSIGN                                                       !5, ~31
         67      > JMP                                                          ->87
   51    68    >   INIT_FCALL                                                   'str_split_offset'
         69        SEND_VAR                                                     !3
         70        SEND_VAR                                                     !4
         71        SEND_VAR                                                     !5
         72        SEND_VAL                                                     '+'
         73        DO_FCALL                                          0  $33     
         74        ASSIGN                                                       !1, $33
   52    75        INIT_FCALL                                                   'str_pad'
         76        SEND_VAR                                                     !5
         77        SEND_VAL                                                     3
         78        SEND_VAL                                                     '+'
         79        SEND_VAL                                                     0
         80        DO_ICALL                                             $35     
         81        ECHO                                                         $35
         82        ROPE_INIT                                         3  ~37     '+%7C'
         83        ROPE_ADD                                          1  ~37     ~37, !1
         84        ROPE_END                                          2  ~36     ~37, '%7C%0A'
         85        ECHO                                                         ~36
   50    86        PRE_INC                                                      !5
         87    >   MUL                                                  ~40     !4, 2
         88        IS_SMALLER_OR_EQUAL                                          !5, ~40
         89      > JMPNZ                                                        ~41, ->68
   55    90    >   ECHO                                                         '%0AOffset+from+right%0A'
   56    91        MUL                                                  ~42     !4, -1
         92        MUL                                                  ~43     ~42, 2
         93        ASSIGN                                                       !5, ~43
         94      > JMP                                                          ->121
   57    95    >   INIT_FCALL                                                   'str_split_offset'
         96        SEND_VAR                                                     !3
         97        MUL                                                  ~45     !4, -1
         98        SEND_VAL                                                     ~45
         99        SEND_VAR                                                     !5
        100        SEND_VAL                                                     '+'
        101        DO_FCALL                                          0  $46     
        102        ASSIGN                                                       !1, $46
   58   103        INIT_FCALL                                                   'str_pad'
        104        SEND_VAR                                                     !5
        105        SEND_VAL                                                     3
        106        SEND_VAL                                                     '+'
        107        SEND_VAL                                                     0
        108        DO_ICALL                                             $48     
        109        ECHO                                                         $48
        110        INIT_FCALL                                                   'str_pad'
        111        ROPE_INIT                                         3  ~50     '%7C'
        112        ROPE_ADD                                          1  ~50     ~50, !1
        113        ROPE_END                                          2  ~49     ~50, '%7C%0A'
        114        SEND_VAL                                                     ~49
        115        SEND_VAL                                                     20
        116        SEND_VAL                                                     '+'
        117        SEND_VAL                                                     0
        118        DO_ICALL                                             $52     
        119        ECHO                                                         $52
   56   120        PRE_INC                                                      !5
        121    >   MUL                                                  ~54     !4, 2
        122        IS_SMALLER_OR_EQUAL                                          !5, ~54
        123      > JMPNZ                                                        ~55, ->95
   59   124    > > RETURN                                                       1

Function str_split_offset:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 13
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 28
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
2 jumps found. (Code = 43) Position 1 = 47, Position 2 = 50
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 51
Branch analysis from position: 51
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 50
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 47, Position 2 = 50
Branch analysis from position: 47
Branch analysis from position: 50
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 16
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 28
Branch analysis from position: 22
Branch analysis from position: 28
Branch analysis from position: 16
filename:       /in/7tf69
function name:  str_split_offset
number of ops:  53
compiled vars:  !0 = $str, !1 = $chunk_len, !2 = $offset, !3 = $joiner, !4 = $len, !5 = $a
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    4     0  E >   RECV                                                 !0      
          1        RECV_INIT                                            !1      1
          2        RECV_INIT                                            !2      0
          3        RECV_INIT                                            !3      null
    5     4        STRLEN                                               ~6      !0
          5        ASSIGN                                                       !4, ~6
    7     6        IS_SMALLER                                                   !1, 0
          7      > JMPZ                                                         ~8, ->13
    8     8    >   MUL                                                  ~9      !1, -1
          9        ASSIGN                                                       !1, ~9
    9    10        SUB                                                  ~11     !4, !2
         11        ASSIGN                                                       !2, ~11
    7    12      > JMP                                                          ->16
   10    13    >   BOOL_NOT                                             ~13     !1
         14      > JMPZ                                                         ~13, ->16
   11    15    >   ASSIGN                                                       !1, 1
   14    16    >   MOD                                                  ~15     !2, !1
         17        ADD                                                  ~16     ~15, !1
         18        MOD                                                  ~17     ~16, !1
         19        ASSIGN                                                       !2, ~17
   16    20        IS_IDENTICAL                                                 !2, 0
         21      > JMPZ                                                         ~19, ->28
   17    22    >   INIT_FCALL                                                   'str_split'
         23        SEND_VAR                                                     !0
         24        SEND_VAR                                                     !1
         25        DO_ICALL                                             $20     
         26        ASSIGN                                                       !5, $20
   16    27      > JMP                                                          ->45
   20    28    >   INIT_FCALL                                                   'str_repeat'
         29        SEND_VAL                                                     '+'
         30        SUB                                                  ~22     !1, !2
         31        SEND_VAL                                                     ~22
         32        DO_ICALL                                             $23     
         33        CONCAT                                               ~24     $23, !0
         34        ASSIGN                                                       !0, ~24
   21    35        INIT_FCALL                                                   'str_split'
         36        SEND_VAR                                                     !0
         37        SEND_VAR                                                     !1
         38        DO_ICALL                                             $26     
         39        ASSIGN                                                       !5, $26
   22    40        FETCH_DIM_R                                          ~29     !5, 0
         41        SUB                                                  ~30     !1, !2
         42        FRAMELESS_ICALL_2                substr              ~31     ~29, ~30
         43        ASSIGN_DIM                                                   !5, 0
         44        OP_DATA                                                      ~31
   30    45    >   TYPE_CHECK                                      1020          !3
         46      > JMPZ                                                         ~32, ->50
         47    >   FRAMELESS_ICALL_2                implode             ~33     !3, !5
         48        QM_ASSIGN                                            ~34     ~33
         49      > JMP                                                          ->51
         50    >   QM_ASSIGN                                            ~34     !5
         51    > > RETURN                                                       ~34
   31    52*     > RETURN                                                       null

End of function str_split_offset

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
121.73 ms | 522 KiB | 16 Q