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 unZH0
Output for 4.3.11 - 4.4.1, 4.4.3 - 5.1.1, 5.1.3 - 7.2.0
Warning: preg_match(): No ending delimiter '^' found in /in/MZHKU on line 14 Warning: preg_match(): No ending delimiter '^' found in /in/MZHKU on line 14 Warning: preg_match(): No ending delimiter '^' found in /in/MZHKU on line 14 Warning: preg_match(): No ending delimiter '^' found in /in/MZHKU on line 14 array(4) { [0]=> array(2) { [0]=> int(1) [1]=> string(6) "2x egg" } [1]=> array(2) { [0]=> int(1) [1]=> string(10) "2 - carrot" } [2]=> array(2) { [0]=> int(1) [1]=> string(7) "cabbage" } [3]=> array(2) { [0]=> int(1) [1]=> string(9) "1x potato" } }
Output for 4.4.2, 5.1.2
Warning: preg_match(): No ending delimiter '^' found in /in/MZHKU on line 15 Warning: preg_match(): No ending delimiter '^' found in /in/MZHKU on line 15 Warning: preg_match(): No ending delimiter '^' found in /in/MZHKU on line 15 Warning: preg_match(): No ending delimiter '^' found in /in/MZHKU on line 15 array(4) { [0]=> array(2) { [0]=> int(1) [1]=> string(6) "2x egg" } [1]=> array(2) { [0]=> int(1) [1]=> string(10) "2 - carrot" } [2]=> array(2) { [0]=> int(1) [1]=> string(7) "cabbage" } [3]=> array(2) { [0]=> int(1) [1]=> string(9) "1x potato" } }
Output for 4.3.0 - 4.3.10
Warning: No ending delimiter '^' found in /in/MZHKU on line 14 Warning: No ending delimiter '^' found in /in/MZHKU on line 14 Warning: No ending delimiter '^' found in /in/MZHKU on line 14 Warning: No ending delimiter '^' found in /in/MZHKU on line 14 array(4) { [0]=> array(2) { [0]=> int(1) [1]=> string(6) "2x egg" } [1]=> array(2) { [0]=> int(1) [1]=> string(10) "2 - carrot" } [2]=> array(2) { [0]=> int(1) [1]=> string(7) "cabbage" } [3]=> array(2) { [0]=> int(1) [1]=> string(9) "1x potato" } }