3v4l.org

run code in 300+ PHP versions simultaneously
<?php function do_split($name) { return preg_split('/([A-Z]{2,}|([A-Z][^A-Z]*))/', $name, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); } function convert_case($name) { return implode( '_', array_map( 'strtolower', do_split($name) ) ); } function convert_case2($in) { $len = strlen($in); if ($len < 2) { return strtolower($in); } $pos = 0; $out = ''; $peek = function() { if ($pos+1 >= $len) { return null; } return $in[$pos+1]; }; $upper = function ($c) { return 'A' <= $c && $c <= 'Z'; }; $lower = function ($c) { return 'a' <= $c && $c <= 'z'; }; $alpha = function ($c) use ($upper, $lower) { return $upper($c) || $lower($c); }; $num = function ($c) { return '0' <= $c && $c <= '9'; }; $alpha_num = function ($c) use ($alpha, $num) { return $alpha($c) || $num($c); }; $start_state = function () { return null; }; $state = $start_state; while ($state !== null) { $state = $state(); } return strtolower($out); } convert_case2('FooBARbar0123'); $samples = array( 'FooBar', 'FooBAR', 'FOOBar', 'foo_bar', 'FOO_bar', 'FOO_Bar', 'foo_BAR', 'foo_Bar', 'Foo_bar', ); print_r(array_map(function ($name) { return array( 'name' => $name, 'converted' => convert_case2($name), // 'converted' => convert_case($name), ); }, $samples));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6kkua
function name:  (null)
number of ops:  13
compiled vars:  !0 = $samples
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   INIT_FCALL                                               'convert_case2'
          1        SEND_VAL                                                 'FooBARbar0123'
          2        DO_FCALL                                      0          
   59     3        ASSIGN                                                   !0, <array>
   73     4        INIT_FCALL                                               'print_r'
          5        INIT_FCALL                                               'array_map'
          6        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F6kkua%3A73%247'
   79     7        SEND_VAL                                                 ~3
          8        SEND_VAR                                                 !0
          9        DO_ICALL                                         $4      
         10        SEND_VAR                                                 $4
         11        DO_ICALL                                                 
         12      > RETURN                                                   1

Function do_split:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6kkua
function name:  do_split
number of ops:  9
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        INIT_FCALL                                               'preg_split'
          2        SEND_VAL                                                 '%2F%28%5BA-Z%5D%7B2%2C%7D%7C%28%5BA-Z%5D%5B%5EA-Z%5D%2A%29%29%2F'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 null
          5        SEND_VAL                                                 3
          6        DO_ICALL                                         $1      
          7      > RETURN                                                   $1
    5     8*     > RETURN                                                   null

End of function do_split

Function convert_case:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6kkua
function name:  convert_case
number of ops:  14
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
    8     1        INIT_FCALL                                               'implode'
    9     2        SEND_VAL                                                 '_'
   10     3        INIT_FCALL                                               'array_map'
   11     4        SEND_VAL                                                 'strtolower'
   12     5        INIT_FCALL                                               'do_split'
          6        SEND_VAR                                                 !0
          7        DO_FCALL                                      0  $1      
          8        SEND_VAR                                                 $1
          9        DO_ICALL                                         $2      
         10        SEND_VAR                                                 $2
         11        DO_ICALL                                         $3      
         12      > RETURN                                                   $3
   15    13*     > RETURN                                                   null

End of function convert_case

Function convert_case2:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
2 jumps found. (Code = 44) Position 1 = 36, Position 2 = 31
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
2 jumps found. (Code = 44) Position 1 = 36, Position 2 = 31
Branch analysis from position: 36
Branch analysis from position: 31
filename:       /in/6kkua
function name:  convert_case2
number of ops:  41
compiled vars:  !0 = $in, !1 = $len, !2 = $pos, !3 = $out, !4 = $peek, !5 = $upper, !6 = $lower, !7 = $alpha, !8 = $num, !9 = $alpha_num, !10 = $start_state, !11 = $state
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
   18     1        STRLEN                                           ~12     !0
          2        ASSIGN                                                   !1, ~12
   19     3        IS_SMALLER                                               !1, 2
          4      > JMPZ                                                     ~14, ->9
   20     5    >   INIT_FCALL                                               'strtolower'
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $15     
          8      > RETURN                                                   $15
   22     9    >   ASSIGN                                                   !2, 0
   23    10        ASSIGN                                                   !3, ''
   24    11        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F6kkua%3A24%240'
         12        ASSIGN                                                   !4, ~18
   30    13        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F6kkua%3A30%241'
         14        ASSIGN                                                   !5, ~20
   33    15        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F6kkua%3A33%242'
         16        ASSIGN                                                   !6, ~22
   36    17        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F6kkua%3A36%243'
         18        BIND_LEXICAL                                             ~24, !5
         19        BIND_LEXICAL                                             ~24, !6
         20        ASSIGN                                                   !7, ~24
   39    21        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F6kkua%3A39%244'
         22        ASSIGN                                                   !8, ~26
   42    23        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F6kkua%3A42%245'
         24        BIND_LEXICAL                                             ~28, !7
         25        BIND_LEXICAL                                             ~28, !8
         26        ASSIGN                                                   !9, ~28
   46    27        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F6kkua%3A46%246'
         28        ASSIGN                                                   !10, ~30
   50    29        ASSIGN                                                   !11, !10
   51    30      > JMP                                                      ->34
   52    31    >   INIT_DYNAMIC_CALL                                        !11
         32        DO_FCALL                                      0  $33     
         33        ASSIGN                                                   !11, $33
   51    34    >   TYPE_CHECK                                  1020          !11
         35      > JMPNZ                                                    ~35, ->31
   54    36    >   INIT_FCALL                                               'strtolower'
         37        SEND_VAR                                                 !3
         38        DO_ICALL                                         $36     
         39      > RETURN                                                   $36
   55    40*     > RETURN                                                   null

End of function convert_case2

Function %00%7Bclosure%7D%2Fin%2F6kkua%3A24%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 4
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6kkua
function name:  {closure}
number of ops:  8
compiled vars:  !0 = $pos, !1 = $len, !2 = $in
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   ADD                                              ~3      !0, 1
          1        IS_SMALLER_OR_EQUAL                                      !1, ~3
          2      > JMPZ                                                     ~4, ->4
   26     3    > > RETURN                                                   null
   28     4    >   ADD                                              ~5      !0, 1
          5        FETCH_DIM_R                                      ~6      !2, ~5
          6      > RETURN                                                   ~6
   29     7*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F6kkua%3A24%240

Function %00%7Bclosure%7D%2Fin%2F6kkua%3A30%241:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 3, Position 2 = 5
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
filename:       /in/6kkua
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
   31     1        IS_SMALLER_OR_EQUAL                              ~1      'A', !0
          2      > JMPZ_EX                                          ~1      ~1, ->5
          3    >   IS_SMALLER_OR_EQUAL                              ~2      !0, 'Z'
          4        BOOL                                             ~1      ~2
          5    > > RETURN                                                   ~1
   32     6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F6kkua%3A30%241

Function %00%7Bclosure%7D%2Fin%2F6kkua%3A33%242:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 3, Position 2 = 5
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
filename:       /in/6kkua
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
   34     1        IS_SMALLER_OR_EQUAL                              ~1      'a', !0
          2      > JMPZ_EX                                          ~1      ~1, ->5
          3    >   IS_SMALLER_OR_EQUAL                              ~2      !0, 'z'
          4        BOOL                                             ~1      ~2
          5    > > RETURN                                                   ~1
   35     6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F6kkua%3A33%242

Function %00%7Bclosure%7D%2Fin%2F6kkua%3A36%243:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 7, Position 2 = 11
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/6kkua
function name:  {closure}
number of ops:  13
compiled vars:  !0 = $c, !1 = $upper, !2 = $lower
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
   37     3        INIT_DYNAMIC_CALL                                        !1
          4        SEND_VAR_EX                                              !0
          5        DO_FCALL                                      0  $3      
          6      > JMPNZ_EX                                         ~4      $3, ->11
          7    >   INIT_DYNAMIC_CALL                                        !2
          8        SEND_VAR_EX                                              !0
          9        DO_FCALL                                      0  $5      
         10        BOOL                                             ~4      $5
         11    > > RETURN                                                   ~4
   38    12*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F6kkua%3A36%243

Function %00%7Bclosure%7D%2Fin%2F6kkua%3A39%244:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 3, Position 2 = 5
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
filename:       /in/6kkua
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   RECV                                             !0      
   40     1        IS_SMALLER_OR_EQUAL                              ~1      '0', !0
          2      > JMPZ_EX                                          ~1      ~1, ->5
          3    >   IS_SMALLER_OR_EQUAL                              ~2      !0, '9'
          4        BOOL                                             ~1      ~2
          5    > > RETURN                                                   ~1
   41     6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F6kkua%3A39%244

Function %00%7Bclosure%7D%2Fin%2F6kkua%3A42%245:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 7, Position 2 = 11
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/6kkua
function name:  {closure}
number of ops:  13
compiled vars:  !0 = $c, !1 = $alpha, !2 = $num
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
   43     3        INIT_DYNAMIC_CALL                                        !1
          4        SEND_VAR_EX                                              !0
          5        DO_FCALL                                      0  $3      
          6      > JMPNZ_EX                                         ~4      $3, ->11
          7    >   INIT_DYNAMIC_CALL                                        !2
          8        SEND_VAR_EX                                              !0
          9        DO_FCALL                                      0  $5      
         10        BOOL                                             ~4      $5
         11    > > RETURN                                                   ~4
   44    12*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F6kkua%3A42%245

Function %00%7Bclosure%7D%2Fin%2F6kkua%3A46%246:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6kkua
function name:  {closure}
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E > > RETURN                                                   null
   48     1*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F6kkua%3A46%246

Function %00%7Bclosure%7D%2Fin%2F6kkua%3A73%247:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6kkua
function name:  {closure}
number of ops:  8
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   73     0  E >   RECV                                             !0      
   75     1        INIT_ARRAY                                       ~1      !0, 'name'
   76     2        INIT_FCALL                                               'convert_case2'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $2      
          5        ADD_ARRAY_ELEMENT                                ~1      $2, 'converted'
          6      > RETURN                                                   ~1
   79     7*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F6kkua%3A73%247

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
156.77 ms | 960 KiB | 27 Q