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); } // no state $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); }; // stream state $pos = 0; $char = $in[0]; $peek = function() use ($in, &$pos, $len) { if ($pos+1 >= $len) { return null; } return $in[$pos+1]; }; $next = function() use ($peek, &$pos, &$char) { $char = $peek(); $pos++; return $char !== null; }; // lex state $out = ''; $find_start = function () use (&$char, $alpha_num, $next) { while (!$alpha_num($char)) { if (!$next()) { return null; } } echo "START: $char"; return null; }; $state = $find_start; 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/l49l2
function name:  (null)
number of ops:  13
compiled vars:  !0 = $samples
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   74     0  E >   INIT_FCALL                                               'convert_case2'
          1        SEND_VAL                                                 'FooBARbar0123'
          2        DO_FCALL                                      0          
   76     3        ASSIGN                                                   !0, <array>
   90     4        INIT_FCALL                                               'print_r'
          5        INIT_FCALL                                               'array_map'
          6        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fl49l2%3A90%248'
   96     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/l49l2
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/l49l2
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 = 47
Branch analysis from position: 47
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 44
Branch analysis from position: 49
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 44
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 44
Branch analysis from position: 49
Branch analysis from position: 44
filename:       /in/l49l2
function name:  convert_case2
number of ops:  54
compiled vars:  !0 = $in, !1 = $len, !2 = $upper, !3 = $lower, !4 = $alpha, !5 = $num, !6 = $alpha_num, !7 = $pos, !8 = $char, !9 = $peek, !10 = $next, !11 = $out, !12 = $find_start, !13 = $state
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
   18     1        STRLEN                                           ~14     !0
          2        ASSIGN                                                   !1, ~14
   19     3        IS_SMALLER                                               !1, 2
          4      > JMPZ                                                     ~16, ->9
   20     5    >   INIT_FCALL                                               'strtolower'
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $17     
          8      > RETURN                                                   $17
   24     9    >   DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fl49l2%3A24%240'
         10        ASSIGN                                                   !2, ~18
   27    11        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fl49l2%3A27%241'
         12        ASSIGN                                                   !3, ~20
   30    13        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fl49l2%3A30%242'
         14        BIND_LEXICAL                                             ~22, !2
         15        BIND_LEXICAL                                             ~22, !3
         16        ASSIGN                                                   !4, ~22
   33    17        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fl49l2%3A33%243'
         18        ASSIGN                                                   !5, ~24
   36    19        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fl49l2%3A36%244'
         20        BIND_LEXICAL                                             ~26, !4
         21        BIND_LEXICAL                                             ~26, !5
         22        ASSIGN                                                   !6, ~26
   41    23        ASSIGN                                                   !7, 0
   42    24        FETCH_DIM_R                                      ~29     !0, 0
         25        ASSIGN                                                   !8, ~29
   43    26        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fl49l2%3A43%245'
         27        BIND_LEXICAL                                             ~31, !0
         28        BIND_LEXICAL                                             ~31, !7
         29        BIND_LEXICAL                                             ~31, !1
         30        ASSIGN                                                   !9, ~31
   49    31        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fl49l2%3A49%246'
         32        BIND_LEXICAL                                             ~33, !9
         33        BIND_LEXICAL                                             ~33, !7
         34        BIND_LEXICAL                                             ~33, !8
         35        ASSIGN                                                   !10, ~33
   56    36        ASSIGN                                                   !11, ''
   57    37        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fl49l2%3A57%247'
         38        BIND_LEXICAL                                             ~36, !8
         39        BIND_LEXICAL                                             ~36, !6
         40        BIND_LEXICAL                                             ~36, !10
         41        ASSIGN                                                   !12, ~36
   67    42        ASSIGN                                                   !13, !12
   68    43      > JMP                                                      ->47
   69    44    >   INIT_DYNAMIC_CALL                                        !13
         45        DO_FCALL                                      0  $39     
         46        ASSIGN                                                   !13, $39
   68    47    >   TYPE_CHECK                                  1020          !13
         48      > JMPNZ                                                    ~41, ->44
   71    49    >   INIT_FCALL                                               'strtolower'
         50        SEND_VAR                                                 !11
         51        DO_ICALL                                         $42     
         52      > RETURN                                                   $42
   72    53*     > RETURN                                                   null

End of function convert_case2

Function %00%7Bclosure%7D%2Fin%2Fl49l2%3A24%240:
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/l49l2
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
   25     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
   26     6*     > RETURN                                                   null

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

Function %00%7Bclosure%7D%2Fin%2Fl49l2%3A27%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/l49l2
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
   28     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
   29     6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Fl49l2%3A27%241

Function %00%7Bclosure%7D%2Fin%2Fl49l2%3A30%242:
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/l49l2
function name:  {closure}
number of ops:  13
compiled vars:  !0 = $c, !1 = $upper, !2 = $lower
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
   31     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
   32    12*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Fl49l2%3A30%242

Function %00%7Bclosure%7D%2Fin%2Fl49l2%3A33%243:
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/l49l2
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      '0', !0
          2      > JMPZ_EX                                          ~1      ~1, ->5
          3    >   IS_SMALLER_OR_EQUAL                              ~2      !0, '9'
          4        BOOL                                             ~1      ~2
          5    > > RETURN                                                   ~1
   35     6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Fl49l2%3A33%243

Function %00%7Bclosure%7D%2Fin%2Fl49l2%3A36%244:
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/l49l2
function name:  {closure}
number of ops:  13
compiled vars:  !0 = $c, !1 = $alpha, !2 = $num
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%2Fl49l2%3A36%244

Function %00%7Bclosure%7D%2Fin%2Fl49l2%3A43%245:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 7
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/l49l2
function name:  {closure}
number of ops:  11
compiled vars:  !0 = $in, !1 = $pos, !2 = $len
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
   44     3        ADD                                              ~3      !1, 1
          4        IS_SMALLER_OR_EQUAL                                      !2, ~3
          5      > JMPZ                                                     ~4, ->7
   45     6    > > RETURN                                                   null
   47     7    >   ADD                                              ~5      !1, 1
          8        FETCH_DIM_R                                      ~6      !0, ~5
          9      > RETURN                                                   ~6
   48    10*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Fl49l2%3A43%245

Function %00%7Bclosure%7D%2Fin%2Fl49l2%3A49%246:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/l49l2
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $peek, !1 = $pos, !2 = $char
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
   50     3        INIT_DYNAMIC_CALL                                        !0
          4        DO_FCALL                                      0  $3      
          5        ASSIGN                                                   !2, $3
   51     6        PRE_INC                                                  !1
   52     7        TYPE_CHECK                                  1020  ~6      !2
          8      > RETURN                                                   ~6
   53     9*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Fl49l2%3A49%246

Function %00%7Bclosure%7D%2Fin%2Fl49l2%3A57%247:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 4
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 9
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/l49l2
function name:  {closure}
number of ops:  19
compiled vars:  !0 = $char, !1 = $alpha_num, !2 = $next
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
   58     3      > JMP                                                      ->9
   59     4    >   INIT_DYNAMIC_CALL                                        !2
          5        DO_FCALL                                      0  $3      
          6        BOOL_NOT                                         ~4      $3
          7      > JMPZ                                                     ~4, ->9
   60     8    > > RETURN                                                   null
   58     9    >   INIT_DYNAMIC_CALL                                        !1
         10        SEND_VAR_EX                                              !0
         11        DO_FCALL                                      0  $5      
         12        BOOL_NOT                                         ~6      $5
         13      > JMPNZ                                                    ~6, ->4
   63    14    >   NOP                                                      
         15        FAST_CONCAT                                      ~7      'START%3A+', !0
         16        ECHO                                                     ~7
   64    17      > RETURN                                                   null
   65    18*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Fl49l2%3A57%247

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

End of function %00%7Bclosure%7D%2Fin%2Fl49l2%3A90%248

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
158.71 ms | 1418 KiB | 26 Q