Test for a value in an array and then return that value

Ok, so I have an array of values Lets say format[Cups, Quarts, Pints, TBSP]
Now I have a textarea input from the user that has a list of ingredients in a recipe.
What I am hoping to do is search through the input line by line (one ingredient at a time) and search for theword that matches one of the values in my array. and set it to the $newFormat variable.

example: user input say “2 Cups of water” I need to find Cups in the input and asign it to the Variable $newformat
then, the next line says “1 Quart of BBQ Sauce” so when I run the code again, I need to find Quart and store it as $newformat.

The idea being that for each ingredient, I need to be able to break it up into 3 variables, $amount (the number) $format (Cup, Quart, etc…) and the $ingredient (Ingredient name) so that I can then take those alues and check them against a database that lists what is available.

I know that I can use in_array() to test if the value is in the array, and obviously I should easily be able to break up the input into an array using explode(), but then how would I find the individual parts I am looking for and asign it to the variable I want?

$first = explode(" ", $input) should give me an array where $first[0] = “2”, $first[1] = “Cups”, $first[2] = “of”, and $first[3] = “water”

How do I take that array and filter it out so that I end up with:
$amount = 2
$format = “Cups”
$Ingredient = “Water”

why not just name your form variables amount, format, and ingredient to begin with? Then you can access it as $_POST[‘amount’], $_POST[‘format’], etc.

Because I want the user to be able to input a whole list of ingredients by simply copy/paste from a recipe website or txt file. I don’t want them to have to break it up themselves, I want to do the work for them.

in other words, they input the following:

2 Cups of chicken
1 Quart of BBQ Sauce

I want the code to break up the input rather than have to have 3 seperate fields (amount, format, ingredient) that they have to fill out, and have multiple instances of each for them to use for all the ingredients.

Especially because I have no way of knowing how many ingredients the recipe will have, and so can’t easily provide the correct amount of those fields for them to fill out. A Textarea is much easier to use.

Okay, fair enough, look at http://php.net/list

Ok, that would almost work…

the following code is what I am using so far:

$row = mysql_fetch_row($result);
$ingredients_line = explode("
“, $row[2]);
foreach ($ingredients_line as $ingredients)
echo “<br>”;
$test = explode (” ", $ingredients);
foreach($test as $value)
echo “$value <br>”;

this takes an input like this:
2 Cups of Water
1 Quart of BBQ

and first breaks it up by line, then by word/number. (it prints them out for me, so I can see that it is working correctly)(also note that the input is actually coming from a Database that was populated by user input on a different page)

So to use list() I would need to do the following
list ($amount, $format, , $ingredient) = $test

But here is the problem…what if the input is:

Water 2 Cups
BBQ 1 Quart

because of the different format (which I need to be able to handle) now my $amount would end up being “water” $format would be “2” and $ingredient would be “Cups”

I know I am probably making this more difficult than it needs to be…

Maybe I could change the page that puts the data into the DB to take the input and re-format it to the correct format? That would allow me to use the list() to assign the variables as I need to…I’ll have to lok and see if I can figure out how to do that…

Correct, you would have to re-format the input to match what you expect to receive. This is where it will become very tricky for you, as the user could enter it in a dozen different ways, and coding to that will be hard.

A last option you could do, is on the first submit, don’t process the data, just explode it into their appropriate arrays, then output the information to the user, showing what they entered with a drop down of amount, ingredient, format so the user can tell you which word is the amount, the ingredient, and the format.

You can assume it will be amount, format, ingredient and have the fields pre-populated that way, but this way, the user can fix it if they entered it as Water 2 Cups instead of 2 Cups Water.