3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str ="(10*2)/(3*40)*100"; $str = str_split($str); // make str an array $arr = array(); $j=0; // counter for new array for($i=0;$i<count($str);$i++){ if(is_numeric($str[$i])){ // if the item is a number $arr[$j] = $str[$i]; // add it to new array $k = $i+1; while(is_numeric($str[$k])){ // while it's still a number append to new array item. $arr[$j] .= $str[$k]; $k++; // add one to counter. if($k == count($str)) break; // if counter is out of bounds, break loop. } $j++; // we are done with this item, add one to counter. $i=$k-1; // set new value to $i }else{ // not number, add it to the new array and add one to array counter. $arr[$j] = $str[$i]; $j++; } } var_dump($arr);
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
array(13) { [0]=> string(1) "(" [1]=> string(2) "10" [2]=> string(1) "*" [3]=> string(1) "2" [4]=> string(1) ")" [5]=> string(1) "/" [6]=> string(1) "(" [7]=> string(1) "3" [8]=> string(1) "*" [9]=> string(2) "40" [10]=> string(1) ")" [11]=> string(1) "*" [12]=> string(3) "100" }

preferences:
214.86 ms | 404 KiB | 290 Q