我的问题是,在使用str_replace时,使用数组是否比多次执行更快。我的问题仅适用于两个替换。
用数组实现
用数组实现
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables");
$yummy = array("pizza", "beer");
$newphrase = str_replace($healthy, $yummy, $phrase);
每个搜索词只能出现一次
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$newphrase = str_replace("fruits", "pizza", $phrase);
$newphrase = str_replace("vegetables", "beer", $phrase);
$phrase = "每天你应该吃比萨、啤酒和纤维。";
- Peter Ajtai