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() use ($in, $pos, $len) { if ($pos+1 >= $len) { return null; } return $in[$pos+1]; }; $next = function() use ($peek, &$pos) { $c = $peek(); if ($c === null) { return null; } $pos++; return $c; }; $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 () use (&$out, $next, $peek) { for ($c = $next(); $c !== null; $c = next()) { $out .= $c; } 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/gFMlR
function name:  (null)
number of ops:  13
compiled vars:  !0 = $samples
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   68     0  E >   INIT_FCALL                                               'convert_case2'
          1        SEND_VAL                                                 'FooBARbar0123'
          2        DO_FCALL                                      0          
   70     3        ASSIGN                                                   !0, <array>
   84     4        INIT_FCALL                                               'print_r'
          5        INIT_FCALL                                               'array_map'
          6        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FgFMlR%3A84%248'
   90     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/gFMlR
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/gFMlR
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 = 44
Branch analysis from position: 44
2 jumps found. (Code = 44) Position 1 = 46, Position 2 = 41
Branch analysis from position: 46
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 41
2 jumps found. (Code = 44) Position 1 = 46, Position 2 = 41
Branch analysis from position: 46
Branch analysis from position: 41
filename:       /in/gFMlR
function name:  convert_case2
number of ops:  51
compiled vars:  !0 = $in, !1 = $len, !2 = $pos, !3 = $out, !4 = $peek, !5 = $next, !6 = $upper, !7 = $lower, !8 = $alpha, !9 = $num, !10 = $alpha_num, !11 = $start_state, !12 = $state
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
   18     1        STRLEN                                           ~13     !0
          2        ASSIGN                                                   !1, ~13
   19     3        IS_SMALLER                                               !1, 2
          4      > JMPZ                                                     ~15, ->9
   20     5    >   INIT_FCALL                                               'strtolower'
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $16     
          8      > RETURN                                                   $16
   22     9    >   ASSIGN                                                   !2, 0
   23    10        ASSIGN                                                   !3, ''
   24    11        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FgFMlR%3A24%240'
         12        BIND_LEXICAL                                             ~19, !0
         13        BIND_LEXICAL                                             ~19, !2
         14        BIND_LEXICAL                                             ~19, !1
         15        ASSIGN                                                   !4, ~19
   30    16        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FgFMlR%3A30%241'
         17        BIND_LEXICAL                                             ~21, !4
         18        BIND_LEXICAL                                             ~21, !2
         19        ASSIGN                                                   !5, ~21
   38    20        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FgFMlR%3A38%242'
         21        ASSIGN                                                   !6, ~23
   41    22        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FgFMlR%3A41%243'
         23        ASSIGN                                                   !7, ~25
   44    24        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FgFMlR%3A44%244'
         25        BIND_LEXICAL                                             ~27, !6
         26        BIND_LEXICAL                                             ~27, !7
         27        ASSIGN                                                   !8, ~27
   47    28        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FgFMlR%3A47%245'
         29        ASSIGN                                                   !9, ~29
   50    30        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FgFMlR%3A50%246'
         31        BIND_LEXICAL                                             ~31, !8
         32        BIND_LEXICAL                                             ~31, !9
         33        ASSIGN                                                   !10, ~31
   54    34        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FgFMlR%3A54%247'
         35        BIND_LEXICAL                                             ~33, !3
         36        BIND_LEXICAL                                             ~33, !5
         37        BIND_LEXICAL                                             ~33, !4
         38        ASSIGN                                                   !11, ~33
   61    39        ASSIGN                                                   !12, !11
   62    40      > JMP                                                      ->44
   63    41    >   INIT_DYNAMIC_CALL                                        !12
         42        DO_FCALL                                      0  $36     
         43        ASSIGN                                                   !12, $36
   62    44    >   TYPE_CHECK                                  1020          !12
         45      > JMPNZ                                                    ~38, ->41
   65    46    >   INIT_FCALL                                               'strtolower'
         47        SEND_VAR                                                 !3
         48        DO_ICALL                                         $39     
         49      > RETURN                                                   $39
   66    50*     > RETURN                                                   null

End of function convert_case2

Function %00%7Bclosure%7D%2Fin%2FgFMlR%3A24%240:
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/gFMlR
function name:  {closure}
number of ops:  11
compiled vars:  !0 = $in, !1 = $pos, !2 = $len
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
   25     3        ADD                                              ~3      !1, 1
          4        IS_SMALLER_OR_EQUAL                                      !2, ~3
          5      > JMPZ                                                     ~4, ->7
   26     6    > > RETURN                                                   null
   28     7    >   ADD                                              ~5      !1, 1
          8        FETCH_DIM_R                                      ~6      !0, ~5
          9      > RETURN                                                   ~6
   29    10*     > RETURN                                                   null

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

Function %00%7Bclosure%7D%2Fin%2FgFMlR%3A30%241:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 8
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/gFMlR
function name:  {closure}
number of ops:  11
compiled vars:  !0 = $peek, !1 = $pos, !2 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
   31     2        INIT_DYNAMIC_CALL                                        !0
          3        DO_FCALL                                      0  $3      
          4        ASSIGN                                                   !2, $3
   32     5        TYPE_CHECK                                    2          !2
          6      > JMPZ                                                     ~5, ->8
   33     7    > > RETURN                                                   null
   35     8    >   PRE_INC                                                  !1
   36     9      > RETURN                                                   !2
   37    10*     > RETURN                                                   null

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

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

End of function %00%7Bclosure%7D%2Fin%2FgFMlR%3A38%242

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

End of function %00%7Bclosure%7D%2Fin%2FgFMlR%3A41%243

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

End of function %00%7Bclosure%7D%2Fin%2FgFMlR%3A44%244

Function %00%7Bclosure%7D%2Fin%2FgFMlR%3A47%245:
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/gFMlR
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   RECV                                             !0      
   48     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
   49     6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FgFMlR%3A47%245

Function %00%7Bclosure%7D%2Fin%2FgFMlR%3A50%246:
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/gFMlR
function name:  {closure}
number of ops:  13
compiled vars:  !0 = $c, !1 = $alpha, !2 = $num
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
   51     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
   52    12*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FgFMlR%3A50%246

Function %00%7Bclosure%7D%2Fin%2FgFMlR%3A54%247:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 7
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 7
Branch analysis from position: 13
Branch analysis from position: 7
filename:       /in/gFMlR
function name:  {closure}
number of ops:  15
compiled vars:  !0 = $out, !1 = $next, !2 = $peek, !3 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   54     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
   55     3        INIT_DYNAMIC_CALL                                        !1
          4        DO_FCALL                                      0  $4      
          5        ASSIGN                                                   !3, $4
          6      > JMP                                                      ->11
   56     7    >   ASSIGN_OP                                     8          !0, !3
   55     8        INIT_FCALL                                               'next'
          9        DO_ICALL                                         $7      
         10        ASSIGN                                                   !3, $7
         11    >   TYPE_CHECK                                  1020          !3
         12      > JMPNZ                                                    ~9, ->7
   58    13    > > RETURN                                                   null
   59    14*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FgFMlR%3A54%247

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

End of function %00%7Bclosure%7D%2Fin%2FgFMlR%3A84%248

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.65 ms | 1410 KiB | 28 Q