【问题标题】:PHP str_replace to replace need with random replacement from array?PHP str_replace 用数组中的随机替换替换需要?
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

    • 这不保证:“不应该相同”。此解决方案可能会在同一块文本中随机打印同一城市。
    【解决方案2】:

    您可以将preg_replace_callbackarray_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;
    

    例如here

    【讨论】:

    • 这不保证:“不应该相同”。此解决方案可能会在同一块文本中随机打印同一城市。
    【解决方案3】:

    这是另一个想法

    $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.
    

    【讨论】:

    • 这不保证:“不应该相同”。此解决方案可能会在同一块文本中随机打印同一城市。
    【解决方案4】:

    您正在使用 $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 的答案。

    【讨论】:

    • "[city] 每次都不应该是同一个 [city]"
    • 谢谢,rour 解决方案非常适合相同的替换。但是,我需要在整个文档中一次性进行变量替换。 :)
    【解决方案5】:

    如果:

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

    您可以将数组作为引用变量传递到自定义函数范围。这意味着当您通过array_pop() 访问和删除值时,不可能两次遇到同一个城市。在执行替换之前,您只需 shuffle() 输入数组一次。

    代码:(Demo)

    $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.
    

    等等

    【讨论】:

      【解决方案6】:

      试试这个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);
      

      【讨论】:

      • 不起作用,因为“[city] 每次都不应该是同一个 [city]”。这对每个替换使用相同的值。
      猜你喜欢
      • 2016-01-11
      • 1970-01-01
      • 1970-01-01
      • 2019-04-19
      • 2013-07-09
      • 2016-10-21
      • 1970-01-01
      • 2012-03-16
      • 1970-01-01
      相关资源
      最近更新 更多