我有多個成分輸入如此設置。按值分解或str_split
3×胡蘿蔔 1/2杯牛奶(或可能是牛奶1/2杯)橄欖油
的 1茶匙和我也有成分和測量的單元的列表,所以我可以檢查是否他們在字符串中。
現在我不知道如何根據從我的成分或值單位名單
所有我能想到的是通過檢查開始,如果的話是像這樣
$listIngredients = // a massive list pulled from the database like below
$units = ['x', 'cup', 'tsp'];
$results = [];
foreach($result->recipe_ingredients as $ingredient)
{
$results = strtr($ingredient, 'of ', '');
$results = explode($ingredient);
// using str_plural() I want to check if ingredient or units of measurement exist. then
// split them if they do so I have an array having the number, then unit, then ingredient
// then I will save each in the database under different columns.
}
在陣列中分割字符串
還是會有更好的方法呢?也許檢查什麼時候有空格或者int停止例如。 3個胡蘿蔔或1/2杯。即時通訊如此困惑> _ <
編輯:我想編號扔掉那裏,我知道爆炸需要2個參數。我的問題更多的是關於什麼參數,如果我需要超過2個,例如我想檢查一個int,unit和一個成分的字符串。
您能提供您期望的簡單輸入和輸出列表嗎?你的問題也是如此令人困惑> _ < – bansi
'$ results = explode($ ingredient); ' - 需要2個參數。你只傳遞一個。 – Naga
'strtr'總是在轉換後返回字符串....並且您正在應用foreach而不是'strtr'的結果 –