2

我已经研究并需要找到最好的方法来随机地用一系列可能性代替需求。

IE:

$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 中的随机值替换所有出现的城市。我想以最干净的方式做到这一点。到目前为止,我的解决方案很糟糕(递归)。什么是最好的解决方案?谢谢!

4

6 回答 6

7

您可以使用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

欢迎来到亚特兰大。我希望达拉斯每次都是随机版本。迈阿密不应该每次都是同一个亚特兰大。

于 2012-06-11T02:29:25.603 回答
5

你可以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;

这里的例子。

于 2012-06-11T02:33:07.287 回答
1

这是另一个想法

$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.
于 2012-06-11T03:02:25.423 回答
0

您正在使用$values数组替换文本,因此结果只是单词“数组”。替换应该是一个字符串。

您可以使用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.RO 的答案。

于 2012-06-11T02:24:28.173 回答
0

如果:

  1. 随机选择的四个城市占位符不应在您的 3 句字符串中重复,并且
  2. 你不介意改变输入数组和
  3. 您将始终有足够的值来容纳所有占位符,然后:

您可以将数组作为引用变量传递到自定义函数范围。这意味着当您通过 访问和删除值时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.

等等

于 2022-01-03T15:04:12.783 回答
-1

试试这个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);
于 2012-06-11T02:31:30.520 回答