【问题标题】:PHP randomly replace wordsPHP随机替换单词
【发布时间】:2012-03-16 17:54:30
【问题描述】:

我有一个名为 $featuresSEO 的数组,其中包含许多单词,例如:

Array (
    [0] => Japan 
    [1] => Japanese 
    [2] => Tokyo 
    [3] => Yokohama 
    [4] => Osaka 
    [5] => Asian 
    [6] => Nagoya 
) 

然后我有一个如下字符串:

 Searching the |*-*| Dating membership database is the key to locating |*-*| people
 you would be interested in. You can search for |*-*| Singles including |*-*| Women
 and |*-*| Men in any location worldwide. Join now to search for |*-*| Singles.

我一直在尝试用数组中的随机单词替换 |*-*| 的实例。我试过 str_replace() 但无法让随机方面工作。

有人能把我推向正确的方向吗?

谢谢

【问题讨论】:

  • 是每个|*-*| 都是随机的,还是选择一个随机词然后用于所有|*-*|
  • 每一个都是随机的词...所以通过字符串不同的词
  • 使用 random() 函数获取随机索引,然后将 |*-*| 替换为您的 Array[your_random_index]

标签: php string random replace


【解决方案1】:

PHP 已经有一个本机函数,它允许您用字符串数组替换模板字符串中的占位符 -- vprintf() 从格式打印变量 如果我没记错的话。

如果您最初使用可识别的占位符设置模板,则无需调用 str_replace()

代码:(Demo)

$template = 'Searching the |*-*| Dating membership database is the key to locating |*-*| people
 you would be interested in. You can search for |*-*| Singles including |*-*| Women
 and |*-*| Men in any location worldwide. Join now to search for |*-*| Singles.';

$array = ['Japan', 'Japanese', 'Tokyo', 'Yokohama', 'Osaka', 'Asian', 'Nagoya'];

shuffle($array);
vprintf(str_replace('|*-*|', '%s', $template), $array);

潜在输出:

Searching the Asian Dating membership database is the key to locating Japan people
 you would be interested in. You can search for Osaka Singles including Nagoya Women
 and Yokohama Men in any location worldwide. Join now to search for Tokyo Singles.

【讨论】:

    【解决方案2】:

    我希望这仍然是相关的)

    $wordarray = [
       'some',
       'random',
       'words',
       'list'
    ];
    shuffle($wordarray);
    $string = implode(' ', $wordarray);
    

    您可以将 implode 更改为 sprintf,这是我的实现。

    【讨论】:

    • OP 希望将随机单词填充到模式中。在这种情况下,它将替换字符数组 |*-*|
    【解决方案3】:

    试试这个

    $string = ' Searching the |*-*| Dating membership database is the key to locating |*-*| people
     you would be interested in. You can search for |*-*| Singles including |*-*| Women
     and |*-*| Men in any location worldwide. Join now to search for |*-*| Singles.';
    
    $words = array('Japanese', 'Tokyo', 'Asian');
    
    $placeholder = '|*-*|';
    $pos = null;
    
    while(null === $pos || false !== $pos) {
        $pos = strpos($string, $placeholder);
        $string = substr_replace($string, $words[rand(0, count($words)-1)], $pos, strlen($placeholder));
    
    }
    
    echo $string;
    

    第一个单词出乎意料

    【讨论】:

      【解决方案4】:

      replace-only-first-match 的代码是从here 借来的。以下代码仅使用列表中的每个单词一次:

      $wordlist = array(
          'Japan',
          'Japanese',
          'Tokyo',
          'Yokohama',
          'Osaka',
          'Asian',
          'Nagoya'
      );
      $string = "
      Searching the |*-*| Dating membership database is the key to locating |*-*| people
      you would be interested in. You can search for |*-*| Singles including |*-*| Women
      and |*-*| Men in any location worldwide. Join now to search for |*-*| Singles.
      ";
      $replace = '|*-*|';
      while(true){
          $index = strpos($string, $replace);
          if($index === false){
              // ran out of place holder strings
              break;
          }
          if(count($wordlist) == 0){
              // ran out of words
              break;
          }
          $word = array_splice($wordlist, rand(0, count($wordlist) - 1), 1);
          $string = substr_replace($string, $word[0], $index, strlen($replace));
      }
      echo $string;
      

      【讨论】:

        【解决方案5】:

        试试这个

        <?php
            $array = array("Japan","Japanese","Tokyo","Yokohama","Osaka","Asian","Nagoya");
            $a = array_rand($array);
            $string= "abc|*-*|";
            echo str_replace("|*-*|", $array[$a], $string);
        ?>
        

        【讨论】:

          【解决方案6】:

          一一替换。这将用一个随机单词替换每个出现。您可能会多次看到来自 $wordarray 的同一个词,因为它每次都会随机选择 1。

          for ($i = 0; $i < substr_count($string, '|*-*|'); $i++){
               $string = preg_replace('/\|\*-\*\|/',$wordarray[rand(0,count($wordarray)-1)],$string, 1);
          }
          

          希望每个单词只使用一次?打乱数组,并循环遍历它:

          shuffle($wordarray);
          foreach ($wordarray as $word){
              $string = preg_replace('/\|\*-\*\|/',$word,$string,1);
          }
          

          【讨论】:

          • 会用第一个随机选择的项目替换所有项目,不是吗?
          • 不。我将1 作为第四个参数。所以它只会替换 1 次出现
          • 第四个参数给了我一个警告!
          • @Topener 最后一个参数需要作为参考
          猜你喜欢
          • 2012-05-29
          • 1970-01-01
          • 2021-05-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-10
          • 2019-04-24
          • 2017-11-03
          相关资源
          最近更新 更多