我进行了研究,需要找到用一系列可能性随机替换需求的最佳方法。
即:
$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 中的随机值替换所有出现的城市。我想以最干净的方式做到这一点。到目前为止我的解决方案很糟糕(递归)。对此最好的解决方案是什么?谢谢!
您可以使用 preg_replace_callback 为每个匹配执行一个函数并返回替换字符串:
$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 = preg_replace_callback('/' . preg_quote($keyword) . '/',
function() use ($values){ return $values[array_rand($values)]; }, $text);
样品
$result
:
欢迎来到亚特兰大。我希望达拉斯每次都是随机版本。迈阿密不应该每次都是同一个亚特兰大。
preg_replace_callback
与 array_rand
一起使用
<?php
$text = "Welcome to [city]. I want [city] to be a random version each time. [city] should not be the same [city] each time.";
$values = array("Orlando", "Dallas", "Atlanta", "Detroit", "Tampa", "Miami");
$result = preg_replace_callback("/\[city\]/", function($matches) use ($values) { return $values[array_rand($values)]; }, $text);
echo $result;
示例此处。
这是另一个想法
$text = "Welcome to [city]. I want [city] to be a random version each time. [city] should not be the same [city] each time.";
$pattern = "/\[city\]/";
$values = array("Orlando", "Dallas", "Atlanta", "Detroit", "Tampa", "Miami");
while(preg_match($pattern, $text)) {
$text = preg_replace($pattern, $values[array_rand($values)], $text, 1);
}
echo $text;
还有一些输出:
Welcome to Orlando. I want Tampa to be a random version each time. Miami should not be the same Orlando each time.
您正在使用
$values
替换文本,它是一个数组,因此结果只是单词“Array”。替换应该是一个字符串。
array_rand()
从数组中随机选择条目。
$result = str_replace($keyword, $values[array_rand($values)], $text);
结果是这样的:
Welcome to Atlanta. I want Atlanta to be a random version each time. Atlanta should not be the same Atlanta each time.
Welcome to Orlando. I want Orlando to be a random version each time. Orlando should not be the same Orlando each time.
如果您希望城市每行是随机的,请检查@PaulP.R.O的答案。
如果:
您可以将数组作为引用变量传递到自定义函数作用域中。 这意味着当您通过
array_pop()
访问和删除值时,不可能两次遇到同一个城市。 在执行替换之前,您只需 shuffle()
输入数组一次。
代码:(演示)
$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 = ["Orlando", "Dallas", "Atlanta", "Detroit", "Tampa", "Miami"];
shuffle($values);
echo preg_replace_callback(
'#' . preg_quote($keyword) . '#',
function() use(&$values) { return array_pop($values); },
$text
);
潜在产出:
Welcome to Detroit. I want Tampa to be a random version each time. Dallas should not be the same Orlando each time.
或
Welcome to Orlando. I want Miami to be a random version each time. Atlanta should not be the same Detroit each time.
或
Welcome to Atlanta. I want Tampa to be a random version each time. Orlando should not be the same Dallas each time.
等等
遵守本答案顶部最初描述的相同规则,将更容易阅读和维护不利用正则表达式的方法。只需对数组进行打乱,将方括号占位符替换为
printf()
系列 if 函数可以识别的占位符,然后将整个打乱后的数组提供给 vprintf()
。
这具有不通过
array_pop()
改变/消耗原始数组的额外好处。 (演示)
shuffle($values);
vprintf(
str_replace(
$keyword,
'%s',
$text
),
$values
);
试试这个http://codepad.org/qp7XYHe4
<?
$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");
echo $result = str_replace($keyword, shuffle($values)?current($values):$values[0], $text);