php str_replace()字符串到数组错误

时间:2013-01-20 13:32:07

标签: php arrays

str_replace($search, $replace, $subject);

$ search string和$ replace string没问题。

但是如何将str_replace()与$ search string和$ replace array一起使用?

1 个答案:

答案 0 :(得分:1)

str_replace() documentation提供了一个明确的示例,说明如何使用数组(大小相同):

// Provides: You should eat pizza, beer, and ice cream every day
$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);

使用$search作为字符串而$replaces作为数组是不可能的并且'怪异':应该将哪个数组值用于匹配?