【问题标题】:php str_replace array by arrayphp str_replace array by array
【发布时间】:2021-08-18 00:40:57
【问题描述】:

字符串替换两个数组只替换第一次出现的每个数组

$text = 'The Book has read by Dog and Cat then Book show Apple not Dog';
$array1 =array('1','2','3','4','5','6');
$array2=array('Book','Dog','Cat','Book','Apple','Dog');
echo str_replace($array2, $array1, $text);

输出已经

The 1 has read by 2 and 3 then 1 show 5 not 2

但需要输出

The 1 has read by 2 and 3 then 4 show 5 not 6

字符串替换数组第 4 和第 6 不能替换它已经重复它必须是第 1 次出现只能替换可能?

【问题讨论】:

    标签: php arrays replace


    【解决方案1】:

    很遗憾,str_replace 没有内置选项来限制替换次数。您可以改用preg_replace,因为此函数的出现次数限制为4th parameter

    您的代码需要考虑到要替换的字符串现在是正则表达式,因此您需要添加一个分隔符(例如 /)。

    下面的工作代码:

    $text = 'The Book has read by Dog and Cat then Book show Apple not Dog';
    $array1 = array('1','2','3','4','5','6');
    $array2 = array('/Book/','/Dog/','/Cat/','/Book/','/Apple/','/Dog/');
    echo preg_replace($array2, $array1, $text, 1);
    

    输出是

    The 1 has read by 2 and 3 then 4 show 5 not 6
    

    【讨论】:

    • 它只是preg_match所需的正则表达式的分隔符。如果您从未使用过正则表达式,您可以搜索很多关于这些的文档,您可以从这里开始:php.net/manual/en/function.preg-replace.php
    【解决方案2】:

    str_replace 期望第一个参数中的针是唯一的(在某种程度上)。因此,它不会寻找Book 的第二次出现,因为它在找到Book 的那一刻,它会在array1 中搜索它的替换索引并立即返回匹配项。

    要克服这个问题,您可以:

    • 将实际句子拆分为标记。
    • 使用array_search 检查array2 中每个令牌的值。
    • 这将返回您的索引。您将令牌值替换为值 在array1 中的此索引处,然后在array1array2 中取消设置此索引。
    • 这样,我们也将能够为重复标记获得新的后续匹配项。

    片段:

    <?php
    
    $text = 'The Book has read by Dog and Cat then Book show Apple not Dog';
    $array1 = array('1','2','3','4','5','6');
    $array2 = array('Book','Dog','Cat','Book','Apple','Dog');
    
    $tokens = explode(" ",$text);
    foreach($tokens as &$token){
        $idx = array_search($token,$array2);
        if($idx !== false){ // if match is found.
            $token = $array1[ $idx ];
            unset($array2[ $idx ]);
            unset($array1[ $idx ]);
        }
    }
    
    echo implode(" ",$tokens);
    

    【讨论】:

      【解决方案3】:

      简单的方法:

      • 使用explode(delimiter, string)$text 转换为数组$output 和分隔符为空格 ,然后循环遍历它。
      • 使用 var $i 来跟踪可替换单词的位置。
      • 在 foreach 中通过引用 &amp; 进行更改。
      • 检查$word 是否在$array2 中,如果则用$array1 中的相应数字更改并增加$i
      • 在后面加上implode(glue, pieces)
      $text = 'The Book has read by Dog and Cat then Book show Apple not Dog';
      $array1 = array('1','2','3','4','5','6');
      $array2 = array('Book','Dog','Cat','Book','Apple','Dog');
      
      $output = explode(' ', $text);
      $i=0;
      foreach ($output as &$word) {
          if(in_array($word, $array2)){
              $word = $array1[$i];
              $i++;
          }
      }
      
      echo implode(' ', $output);
      

      打印:

      //The 1 has read by 2 and 3 then 4 show 5 not 6
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-07-28
        • 2010-10-13
        • 2017-12-17
        • 1970-01-01
        • 2011-08-12
        • 1970-01-01
        • 1970-01-01
        • 2018-08-16
        相关资源
        最近更新 更多