3v4l.org

run code in 300+ 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);

preferences:
29.46 ms | 406 KiB | 5 Q