3v4l.org

run code in 300+ PHP versions simultaneously
<?php //Input string (Ignore the syntax editor, 3v4l's parser can't see that it's a valid HEREDOC string) $str = <<< END 2x egg 2 - carrot cabbage 1x potato 3xyx&& asd - --()(--=) yams 6 mangos 7 whatevers lots of words and stuff, but really though 6 of your finest grapes END; $lines = explode("\n", $str); //Split the input into lines for parsing $ingredients = Array(); //Ready the ingredients array for populating foreach($lines as $line) { $matches = Array(); if(!preg_match("/^.*?(\d+).*?(\S+)(\s+)?$/", $line, $matches)) //Get the number (\d+) from the start (^) of the line and the ingredient name (\S+) from the end ($) of the string array_push($ingredients, Array(1, $line)); //If we can't find the number or ingredient that way just assume it's 1 of whatever the line has in it else array_push($ingredients, Array(intval($matches[1]), $matches[2])); //Otherwise add it to the array with the correct amount and name } var_dump($ingredients);

preferences:
135.55 ms | 407 KiB | 5 Q