0
好吧,如果我使用的代碼:字符串替換爲變量數組
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy = array("pizza", "beer", "ice cream");
$newphrase = str_replace($healthy, $yummy, $phrase);
// Provides: You should eat pizza, beer, and ice cream every day
但是,如果我的話的名單,以取代什麼是可變的。
$sample1 = "fruits, vegetables, fiber";
$sample2 = "pizza, beer, ice cream";
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array($sample1);
$yummy = array($sample2);
$newphrase = str_replace($healthy, $yummy, $phrase);
// Provides: You should eat fruits, vegetables, and fiber every day.
相同的,如果我使用:
$sample1 = '"fruits", "vegetables", "fiber"';
$sample1 = '"pizza", "beer", "ice cream"';
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array($sample1);
$yummy = array($sample2);
$newphrase = str_replace($healthy, $yummy, $phrase);
我缺少的東西?
有沒有辦法輕鬆做到這一點?
什麼是您預期的輸出? – KTAnj