【发布时间】:2012-06-13 23:53:14
【问题描述】:
我已经研究过,需要找到最好的方法来随机地用一系列可能性来代替需求。
即:
$text = "Welcome to [city]. I want [city] to be a random version each time. [city] should not be the same [city] each time.";
$keyword = "[city]";
$values = array("Orlando", "Dallas", "Atlanta", "Detroit", "Tampa", "Miami");
$result = str_replace("[$keyword]", $values, $text);
结果是每次出现的城市都有“数组”。我需要用 $values 中的随机值替换所有出现的城市。我想以最干净的方式做到这一点。到目前为止,我的解决方案很糟糕(递归)。什么是最好的解决方案?谢谢!
【问题讨论】:
标签: php arrays string random str-replace