3v4l.org

run code in 500+ PHP versions simultaneously
<?php function toArray($s){ $operators = array('+','-','*','/','%','(',')'); $finalArr = []; $curStr = ''; foreach (str_split($s) as $char) { if (in_array($char, $operators)) { //Alright, we have an operator. Append the entire number $finalArr[] = $curStr; //And also the operator $finalArr[] = $char; //Reset the current string back to empty $curStr = ''; } else { //Not an operator? Just keep appending the current number $curStr .= $char; } } //Add final leftover string if ($curStr !== '') { //Sanity check here (This shouldn't ever be empty, as you wouldn't end with an operator) $finalArr[] = $curStr; } //Return return $finalArr; } $string = '6+4*4+100+444*6*13*14'; $arr = toArray($string); print_r($arr);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/mDEPu
function name:  (null)
number of ops:  9
compiled vars:  !0 = $string, !1 = $arr
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   32     0  E >   ASSIGN                                                       !0, '6%2B4%2A4%2B100%2B444%2A6%2A13%2A14'
   34     1        INIT_FCALL                                                   'toarray'
          2        SEND_VAR                                                     !0
          3        DO_FCALL                                          0  $3      
          4        ASSIGN                                                       !1, $3
   36     5        INIT_FCALL                                                   'print_r'
          6        SEND_VAR                                                     !1
          7        DO_ICALL                                                     
          8      > RETURN                                                       1

Function toarray:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 19
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 19
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 17
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 24
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
Branch analysis from position: 19
filename:       /in/mDEPu
function name:  toArray
number of ops:  26
compiled vars:  !0 = $s, !1 = $operators, !2 = $finalArr, !3 = $curStr, !4 = $char
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    4     0  E >   RECV                                                 !0      
    5     1        ASSIGN                                                       !1, <array>
    7     2        ASSIGN                                                       !2, <array>
    8     3        ASSIGN                                                       !3, ''
    9     4        INIT_FCALL                                                   'str_split'
          5        SEND_VAR                                                     !0
          6        DO_ICALL                                             $8      
          7      > FE_RESET_R                                           $9      $8, ->19
          8    > > FE_FETCH_R                                                   $9, !4, ->19
   10     9    >   FRAMELESS_ICALL_2                in_array            ~10     !4, !1
         10      > JMPZ                                                         ~10, ->17
   12    11    >   ASSIGN_DIM                                                   !2
         12        OP_DATA                                                      !3
   14    13        ASSIGN_DIM                                                   !2
         14        OP_DATA                                                      !4
   16    15        ASSIGN                                                       !3, ''
   10    16      > JMP                                                          ->18
   19    17    >   ASSIGN_OP                                         8          !3, !4
    9    18    > > JMP                                                          ->8
         19    >   FE_FREE                                                      $9
   24    20        IS_NOT_IDENTICAL                                             !3, ''
         21      > JMPZ                                                         ~15, ->24
   25    22    >   ASSIGN_DIM                                                   !2
         23        OP_DATA                                                      !3
   29    24    > > RETURN                                                       !2
   30    25*     > RETURN                                                       null

End of function toarray

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
139.44 ms | 2658 KiB | 16 Q