【问题标题】:PHP replace sub string with the value from an arrayPHP用数组中的值替换子字符串
【发布时间】: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


    【解决方案1】:

    来,你为什么不试试这个?

    $arr = array("First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eight", "Ninth");
    $arrlength = count($arr);
    $text = "an elephant is an elephant but an elephant is not an elephant";
    $array = explode(" ", $text);
    for ($i=0; $i < count($array); $i++) { 
        if ($array[$i]=="elephant")
        {
            $random_key = array_rand($arr, $arrlength);
            $array[$i] = $arr[$random_key[rand(0, $arrlength-1)]];
        }
    }
    $text = implode(" ", $array);
    echo $text;
    

    【讨论】:

      【解决方案2】:

      这应该适合你:

      使用preg_replace_callback(),您可以简单地使用array_rand() 始终将其替换为数组中的随机值。

      <?php
      
          $arr = array("First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eight", "Ninth");
          $text = "an elephant is an elephant but an elephant is not an elephant";
      
          echo $newText = preg_replace_callback("/\belephant\b/", function($m)use($arr){
              return $arr[array_rand($arr)];
          }, $text);
      
      ?>
      

      可能的输出:

      an Seventh is an Third but an First is not an Ninth
      

      【讨论】:

      • 你先生。拯救了我的一天。这正是我想要的。非常感谢。
      • 最后一件事。你能告诉我文本是否包含“~hic~”而不是“大象”。那我该如何替换“~hic~”?
      • @Illusion 只需将正则表达式:/\belephant\b/ 替换为 /~hic~/
      【解决方案3】:

      对 Rizier123 答案的改编,因此您永远不会有相同的替代品。 (请注意,您不需要替换比数组中的项目数更多的项目)

      $arr = array("First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eight", "Ninth");
      $text = "an elephant is an elephant but an elephant is not an elephant";
      
      echo $newText = preg_replace_callback("/\belephant\b/", function($m)use(&$arr){
        $item = array_rand($arr);  
        $return = $arr[$item];
        unset($arr[$item]);
        return $return;
      }, $text);
      

      【讨论】:

      • 酷.. 但最后一件事。你能告诉我文本是否包含“~hic~”而不是“大象”。那我该如何替换“~hic~”?
      猜你喜欢
      • 2016-06-22
      • 2014-02-01
      • 1970-01-01
      • 2014-02-05
      • 1970-01-01
      • 2019-03-21
      • 1970-01-01
      • 2011-06-26
      • 1970-01-01
      相关资源
      最近更新 更多