Here is a string of characters that I would like to split into an array:
35g walnut halves A handful of thyme leaves 200g portobello mushrooms 200g white mushrooms 200g chifferini pasta 100g Petit Brebis sheep's cheese 40g honey
I would like to use preg_split to extract the individual ingredients from the string. An individual ingredients starts with:
- a quantity defined by digits plus the character
g - a sequence a characters such as
A handful
So far, I have this regex pattern ([0-9]+g|A handful) which correctly finds the breaks in the string, but doesn't include the entire ingredient description. I need the capturing group to include the rest of the characters until the next match.
In order to get the array return, I use this PHP:
preg_split("/([0-9]+g|A handful)/", $ingredients_str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY)
The desired output is:
[
0 => 35g walnut halves
1 => A handful of thyme leaves
2 => 200g portobello mushrooms
etc..
]
See regex 101