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($name) { $length = strlen($name); if ($length < 2) { return strtolower($name); } $output = ''; // init stream $prev_val = '_'; $prev_upr = false; $char_val = $name[0]; $char_upr = ctype_upper($char_val); for ($i = 0; $i < $length; $i++) { // peek $next_val = $i+1 < $length ? $name[$i+1] : '_'; $next_upr = ctype_upper($next_val); if ($prev_val == '_') { if ($char_val == '_') { continue; } $output .= $char_val; } else if ($char_upr !== $prev_upr) { $output .= '_'.$char_val; } else if ($char_upr && !$next_upr) { // && $prev_upr if ($prev_upr) { $output .= '_'.$char_val; $char_upr = false; } else { $output .= $char_val; } } else { $output .= $char_val; } // roll over $prev_val = $char_val; $prev_upr = $char_upr; $char_val = $next_val; $char_upr = $next_upr; } return strtolower($output); } 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/n9NrJ
function name:  (null)
number of ops:  13
compiled vars:  !0 = $samples
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   58     0  E >   INIT_FCALL                                               'convert_case2'
          1        SEND_VAL                                                 'FooBARbar0123'
          2        DO_FCALL                                      0          
   60     3        ASSIGN                                                   !0, <array>
   74     4        INIT_FCALL                                               'print_r'
          5        INIT_FCALL                                               'array_map'
          6        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fn9NrJ%3A74%240'
   80     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/n9NrJ
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/n9NrJ
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 = 62
Branch analysis from position: 62
2 jumps found. (Code = 44) Position 1 = 64, Position 2 = 20
Branch analysis from position: 64
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 27
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 40
Branch analysis from position: 35
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 38
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 61
Branch analysis from position: 61
2 jumps found. (Code = 44) Position 1 = 64, Position 2 = 20
Branch analysis from position: 64
Branch analysis from position: 20
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 57
Branch analysis from position: 57
2 jumps found. (Code = 44) Position 1 = 64, Position 2 = 20
Branch analysis from position: 64
Branch analysis from position: 20
Branch analysis from position: 40
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 45
Branch analysis from position: 42
1 jumps found. (Code = 42) Position 1 = 57
Branch analysis from position: 57
Branch analysis from position: 45
2 jumps found. (Code = 46) Position 1 = 46, Position 2 = 48
Branch analysis from position: 46
2 jumps found. (Code = 43) Position 1 = 49, Position 2 = 56
Branch analysis from position: 49
2 jumps found. (Code = 43) Position 1 = 50, Position 2 = 54
Branch analysis from position: 50
1 jumps found. (Code = 42) Position 1 = 55
Branch analysis from position: 55
1 jumps found. (Code = 42) Position 1 = 57
Branch analysis from position: 57
Branch analysis from position: 54
1 jumps found. (Code = 42) Position 1 = 57
Branch analysis from position: 57
Branch analysis from position: 56
2 jumps found. (Code = 44) Position 1 = 64, Position 2 = 20
Branch analysis from position: 64
Branch analysis from position: 20
Branch analysis from position: 48
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 40
Branch analysis from position: 35
Branch analysis from position: 40
filename:       /in/n9NrJ
function name:  convert_case2
number of ops:  69
compiled vars:  !0 = $name, !1 = $length, !2 = $output, !3 = $prev_val, !4 = $prev_upr, !5 = $char_val, !6 = $char_upr, !7 = $i, !8 = $next_val, !9 = $next_upr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
   18     1        STRLEN                                           ~10     !0
          2        ASSIGN                                                   !1, ~10
   19     3        IS_SMALLER                                               !1, 2
          4      > JMPZ                                                     ~12, ->9
   20     5    >   INIT_FCALL                                               'strtolower'
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $13     
          8      > RETURN                                                   $13
   22     9    >   ASSIGN                                                   !2, ''
   24    10        ASSIGN                                                   !3, '_'
   25    11        ASSIGN                                                   !4, <false>
   26    12        FETCH_DIM_R                                      ~17     !0, 0
         13        ASSIGN                                                   !5, ~17
   27    14        INIT_FCALL                                               'ctype_upper'
         15        SEND_VAR                                                 !5
         16        DO_ICALL                                         $19     
         17        ASSIGN                                                   !6, $19
   28    18        ASSIGN                                                   !7, 0
         19      > JMP                                                      ->62
   30    20    >   ADD                                              ~22     !7, 1
         21        IS_SMALLER                                               ~22, !1
         22      > JMPZ                                                     ~23, ->27
         23    >   ADD                                              ~24     !7, 1
         24        FETCH_DIM_R                                      ~25     !0, ~24
         25        QM_ASSIGN                                        ~26     ~25
         26      > JMP                                                      ->28
         27    >   QM_ASSIGN                                        ~26     '_'
         28    >   ASSIGN                                                   !8, ~26
   31    29        INIT_FCALL                                               'ctype_upper'
         30        SEND_VAR                                                 !8
         31        DO_ICALL                                         $28     
         32        ASSIGN                                                   !9, $28
   32    33        IS_EQUAL                                                 !3, '_'
         34      > JMPZ                                                     ~30, ->40
   33    35    >   IS_EQUAL                                                 !5, '_'
         36      > JMPZ                                                     ~31, ->38
   34    37    > > JMP                                                      ->61
   36    38    >   ASSIGN_OP                                     8          !2, !5
         39      > JMP                                                      ->57
   37    40    >   IS_NOT_IDENTICAL                                         !6, !4
         41      > JMPZ                                                     ~33, ->45
   38    42    >   CONCAT                                           ~34     '_', !5
         43        ASSIGN_OP                                     8          !2, ~34
         44      > JMP                                                      ->57
   39    45    > > JMPZ_EX                                          ~36     !6, ->48
         46    >   BOOL_NOT                                         ~37     !9
         47        BOOL                                             ~36     ~37
         48    > > JMPZ                                                     ~36, ->56
   40    49    > > JMPZ                                                     !4, ->54
   41    50    >   CONCAT                                           ~38     '_', !5
         51        ASSIGN_OP                                     8          !2, ~38
   42    52        ASSIGN                                                   !6, <false>
         53      > JMP                                                      ->55
   44    54    >   ASSIGN_OP                                     8          !2, !5
         55    > > JMP                                                      ->57
   47    56    >   ASSIGN_OP                                     8          !2, !5
   50    57    >   ASSIGN                                                   !3, !5
   51    58        ASSIGN                                                   !4, !6
   52    59        ASSIGN                                                   !5, !8
   53    60        ASSIGN                                                   !6, !9
   28    61    >   PRE_INC                                                  !7
         62    >   IS_SMALLER                                               !7, !1
         63      > JMPNZ                                                    ~48, ->20
   55    64    >   INIT_FCALL                                               'strtolower'
         65        SEND_VAR                                                 !2
         66        DO_ICALL                                         $49     
         67      > RETURN                                                   $49
   56    68*     > RETURN                                                   null

End of function convert_case2

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

End of function %00%7Bclosure%7D%2Fin%2Fn9NrJ%3A74%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
160.05 ms | 1410 KiB | 28 Q