【发布时间】:2015-08-04 09:12:21
【问题描述】:
我有一段文字说:
$text = "An Elephant is an Elephant but an Elephant is not an Elephant"
我有一个数组说:
$array = array("First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eight", "Ninth");
在文本中,您可以看到“大象”出现了很多次。我想要做的是我想用数组中的唯一值替换 Elephant 的出现,结果应该是这样的:
$result = "An Fifth is an Seventh but an First is not an Fourth"
到目前为止我已经尝试过了:
$arr = array("First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eight", "Ninth");
$text = "an elephant is an elephant but an elephant is not an elephant";
$array = explode(" ", $text);
$new_arr = array_diff($array, array("elephant"));
$text = implode(" ".$arr[array_rand($arr)]." ", $new_arr);
echo $text;
它输出如下内容:
an First is First an First but First an First is First not First an
我怎么会变成这样?
An Fifth is an Seventh but an First is not an Fourth
【问题讨论】:
标签: php arrays string str-replace