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 && $next_val !== '_') { // && $prev_upr $output .= $char_val.'_'; $char_val = '_'; $char_upr = false; } 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, 'split' => do_split($name), 'converted' => convert_case($name), ); }, $samples)); */
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BJDgY
function name:  (null)
number of ops:  5
compiled vars:  !0 = $samples
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   55     0  E >   INIT_FCALL                                               'convert_case2'
          1        SEND_VAL                                                 'FooBARbar0123'
          2        DO_FCALL                                      0          
   57     3        ASSIGN                                                   !0, <array>
   78     4      > RETURN                                                   1

Function do_split:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BJDgY
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/BJDgY
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 = 63
Branch analysis from position: 63
2 jumps found. (Code = 44) Position 1 = 65, Position 2 = 20
Branch analysis from position: 65
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 = 62
Branch analysis from position: 62
2 jumps found. (Code = 44) Position 1 = 65, Position 2 = 20
Branch analysis from position: 65
Branch analysis from position: 20
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 58
Branch analysis from position: 58
2 jumps found. (Code = 44) Position 1 = 65, Position 2 = 20
Branch analysis from position: 65
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 = 58
Branch analysis from position: 58
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 = 46) Position 1 = 49, Position 2 = 51
Branch analysis from position: 49
2 jumps found. (Code = 43) Position 1 = 52, Position 2 = 57
Branch analysis from position: 52
1 jumps found. (Code = 42) Position 1 = 58
Branch analysis from position: 58
Branch analysis from position: 57
2 jumps found. (Code = 44) Position 1 = 65, Position 2 = 20
Branch analysis from position: 65
Branch analysis from position: 20
Branch analysis from position: 51
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/BJDgY
function name:  convert_case2
number of ops:  70
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                                                      ->63
   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                                                      ->62
   36    38    >   ASSIGN_OP                                     8          !2, !5
         39      > JMP                                                      ->58
   37    40    >   IS_NOT_IDENTICAL                                         !6, !4
         41      > JMPZ                                                     ~33, ->45
   38    42    >   CONCAT                                           ~34     '_', !5
         43        ASSIGN_OP                                     8          !2, ~34
         44      > JMP                                                      ->58
   39    45    > > JMPZ_EX                                          ~36     !6, ->48
         46    >   BOOL_NOT                                         ~37     !9
         47        BOOL                                             ~36     ~37
         48    > > JMPZ_EX                                          ~36     ~36, ->51
         49    >   IS_NOT_IDENTICAL                                 ~38     !8, '_'
         50        BOOL                                             ~36     ~38
         51    > > JMPZ                                                     ~36, ->57
   40    52    >   CONCAT                                           ~39     !5, '_'
         53        ASSIGN_OP                                     8          !2, ~39
   41    54        ASSIGN                                                   !5, '_'
   42    55        ASSIGN                                                   !6, <false>
         56      > JMP                                                      ->58
   44    57    >   ASSIGN_OP                                     8          !2, !5
   47    58    >   ASSIGN                                                   !3, !5
   48    59        ASSIGN                                                   !4, !6
   49    60        ASSIGN                                                   !5, !8
   50    61        ASSIGN                                                   !6, !9
   28    62    >   PRE_INC                                                  !7
         63    >   IS_SMALLER                                               !7, !1
         64      > JMPNZ                                                    ~49, ->20
   52    65    >   INIT_FCALL                                               'strtolower'
         66        SEND_VAR                                                 !2
         67        DO_ICALL                                         $50     
         68      > RETURN                                                   $50
   53    69*     > RETURN                                                   null

End of function convert_case2

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
160.9 ms | 952 KiB | 26 Q