【问题标题】:PHP: How to walk through string while randomly replacing terms?PHP:如何在随机替换术语时遍历字符串?
【发布时间】:2014-03-14 10:41:21
【问题描述】:

我有一个字符串,它是这样的一段文本:

$input = AAAA BBBB CCCC DDD E FFFF GGG AAAA HHHH IIII JJJ KKKK

我也有这样的数组:

$terms[0][0] = "AAAA";
$terms[0][1] = "KKKK";
$terms[0][2] = "A2A2";
$terms[1][0] = "FFFF";
$terms[1][1] = "TTT";
$terms[1][2] = "FFF1";

这些值既不真实也不重要。该数组由同义词组成。这意味着所有$terms[0] 键具有相同的含义。我的重点是用来自同一 $term 键的同义词随机替换 $string 中的匹配项,例如:

$output == KKKK BBBB CCCC DDD E FF1F GGG A2A2 HHH III JJJ AAAA

$output == A2A2 BBBB CCCC DDD E TTT GGG KKKK HHH III JJJ AAAA

$output == A2A2 BBBB CCCC DDD E FFF1 GGG KKKK HHH III JJJ A2A2

我该怎么做?

我所做的就是循环遍历 $terms[][] 并为每个匹配项构建一个 replace_pairs 数组,其中包含来自相同前一个键的随机值,然后像这样使用 strtr()

$replace_pairs[AAAA] = "KKKK";
$replace_pairs[FFFF] = "TTT";
$replace_pairs[KKKK] = "A2A2";
$output = strtr($input, $replace_pairs);

但是,这会将 所有 相等的匹配项(在本例中为 AAAA 到 KKKK)替换为相同的值,如下所示:

$output == KKKK BBBB CCCC DDD E TTTF GGG KKKK HHH III JJJ A2A2

在“遍历”字符串时有什么方法可以替换,这样我就可以随时从同义词的数组中获取一个新的随机元素?请记住,同义词也有不同的长度。

【问题讨论】:

    标签: php arrays random


    【解决方案1】:

    您可以使用 array_* 和 lambda 函数有效地做到这一点:

    <?php
    
    // This is the same format of your $terms array.    
    $synonyms   = array(
        array(
            'have','possess'
        ),
        array(
            'nice','good','wonderful','pleasurable',
        ),
        array(
            'day','tide','period',
        )
    );
    
    $input = "have a nice day.\n";
    
    function synonymize($input, $synonyms) {
    
        // Replace each word (word-boundary word-characters word-boundary)
        return preg_replace_callback('#\\b(\\w+)\\b#', function($matches) 
            use ($synonyms) {
    
            $word   = $matches[1];
    
            // Find what synonyms, if any, this word has.
            $poss   = array_filter($synonyms, function($candidates) use ($word) {
                return in_array($word, $candidates);
            });
    
            // If none, we can't replace.
            if (empty($poss)) {
                return $word;
            }
    
            $among  = array_pop($poss);
    
            // Otherwise return the alternative.
    
            $newword = $among[rand(0, count($among)-1)];
            // log("replacing {$word} with {$newword}");
            return $newword;
    
        },
        $input);
    }
    
    for ($i = 0; $i < 8; $i++) { print synonymize($input, $synonyms); }
    ?>
    

    产量:

    have a pleasurable day.
    have a nice day.              // Note: null substitution.
    possess a wonderful tide.
    possess a wonderful period.
    possess a good period.
    possess a nice tide.
    have a nice period.           // Note: poor choice of synonyms.
    have a pleasurable day.       // Note: repetition (of course).
    

    【讨论】:

      【解决方案2】:

      试试这个:

      $input = "AAAA BBBB CCCC DDD E FFFF GGG AAAA HHHH IIII JJJ KKKK";
      $terms[0][0] = "AAAA";
      $terms[0][1] = "KKKK";
      $terms[0][2] = "A2A2";
      $terms[1][0] = "FFFF";
      $terms[1][1] = "TTT";
      $terms[1][2] = "FFF1";
      
      $inputarr = explode(" ", $input);
      $outputarr = array();
      foreach($inputarr as $num => $element){
          $outputarr[$num] = $element;
          foreach($terms as $keys){
              if (in_array($element, $keys)){
                  $outputarr[$num] = $keys[rand(0,(count($keys) - 1))];
                  break 1;
              }
          }
      }
      $output = implode(" ", $outputarr);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-29
        • 1970-01-01
        • 2017-04-01
        • 2017-05-20
        • 2019-05-29
        • 2018-07-06
        • 1970-01-01
        • 2017-03-08
        相关资源
        最近更新 更多