- array_push: documentation ( source)
- var_dump: documentation ( source)
- preg_match: documentation ( source)
- explode: documentation ( source)
<?php
$str = <<< END
2x egg
2 - carrot
cabbage
1x potato
3xyx&& asd - --()(--=) yams
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);