Here is an example of a string my application could receive:
2 eggs 24oz sweet potatoes 3 tbsp honey
You'll see there are 3 items in the list, there could be 1 or N (ie: 2 eggs, OR what you see above)
What I'd like to get is an array that has measurement and label, what I'm getting right now is close, but not 100%
//Expected Result
[0]=>['2','24oz','3 tbsp'],
[1]=>['eggs','sweet potatoes','honey']
//Actual Result, 'measurements' are in both arrays, 'values' are not...
[0]=>['2 eggs','24oz','3 tbsp']
[1]=>['2 eggs','24oz','3 tbsp']
//regex being used:
$test = '2 eggs 24oz sweet potatoes 3 tbsp honey ';
preg_match_all('!(\d+\s?\S+)!', $test, $matches);
Any help would be most appreciated!
This will work with your example string:
(\d+(?:\s?\w+)?)((?:\s+[^\d]+)+)
It grabs the amount, and if there is more than one word following, it assumes it's the unit and grabs that as well. Then it grabs the rest, up to any digits (or end of string).
However - even if it works with the example string, it's fragile. Something like 2 veal steaks
would have veal
end up as a unit :S.