3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $str = <<< END 2x egg 2 - carrot cabbage 1x potato END; $lines = explode("\n", $str); $ingredients = Array(); foreach($lines as $line) { $matches = Array(); if(!preg_match("/^(\d+).*?(\S+)$/", $line, $matches)) array_push($ingredients, Array(1, $line)); else array_push($ingredients, Array(intval($matches[1]), $matches[2])); } var_dump($ingredients);
based on nZDqH
Output for 4.3.0 - 7.2.0
array(4) { [0]=> array(2) { [0]=> int(2) [1]=> string(3) "egg" } [1]=> array(2) { [0]=> int(2) [1]=> string(6) "carrot" } [2]=> array(2) { [0]=> int(1) [1]=> string(7) "cabbage" } [3]=> array(2) { [0]=> int(1) [1]=> string(6) "potato" } }