【问题标题】:Replace strings in first array with values from second array用第二个数组中的值替换第一个数组中的字符串
【发布时间】:2010-12-07 15:10:27
【问题描述】:

出于某种原因,我正在为此苦苦挣扎。

我有以下 2 个数组,我需要从 $img 数组中获取数组值并将它们按顺序插入到 $text 数组中,附加/替换 %img_ 标签,如下所示:

$text = array(
    0 => "Bunch of text %img_ %img_: Some more text blabla %img_",
    1 => "More text %img_ blabla %img_"
);

$img = array("BLACK","GREEN","BLUE", "RED", "PINK");

我希望我的 $text 数组最终是这样的:

$text = array(
    0 => "Bunch of text %img_BLACK %img_GREEN: Some moretext blabla %img_BLUE",
    1 => "More text %img_RED blabla %img_PINK"
);

注意:$img 数组中的项目数会有所不同,但始终与 $text 数组中的 %img_ 数相同。

【问题讨论】:

    标签: php regex arrays


    【解决方案1】:

    这是您可以做到的一种方法,使用 preg_replace_callback 和一个类来包装跟踪要使用的 $img 数组中的哪个替换字符串的详细信息:

    class Replacer
    {
        public function __construct($img)
        {
           $this->img=$img;
        }
    
        private function callback($str)
        {
            //this function must return the replacement string
            //for each match - we simply cycle through the 
            //available elements of $this->img.
    
            return '%img_'.$this->img[$this->imgIndex++];
        }
    
        public function replace(&$array)
        {
            $this->imgIndex=0;
    
            foreach($array as $idx=>$str)
            {
                $array[$idx]=preg_replace_callback(
                   '/%img_/', 
                   array($this, 'callback'), 
                   $str);
            }
        } 
    }
    
    //here's how you would use it with your given data
    $r=new Replacer($img);
    $r->replace($text);
    

    【讨论】:

    • 很高兴在这里看到一些 OO 解决方案 :)
    • 啊,谢谢保罗。做的漂亮!帮我省了很多麻烦。
    【解决方案2】:

    OOP 很好。这是我的非 OOP 之一:D

    $text = array(
        0 => "Bunch of text %img_ %img_: Some more text blabla %img_",
        1 => "More text %img_ blabla %img_"
    );
    
    $img = array("BLACK","GREEN","BLUE", "RED", "PINK");
    
    $newtext = array();
    $k = 0;
    $count = count($text);
    for($i = 0; $i < $count; $i++) {
        $texts = split("%img_", $text[$i]);
        $jtext = $texts[0];
        $subcount = count($texts);
        for($j = 1; $j < $subcount; $j++) {
            $jtext .= "%img_";
            $jtext .= $img[$k++];
            $jtext .= $texts[$j];
        }
        $newtext[] = "$jtext\n";
    }
    
    print_r($newtext);
    

    如果你愿意,你可以将它分组到一个函数中。

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      这是另一种方式:

      <?php
      
      $text = array(
          0 => "Bunch of text %img_ %img_: Some more text blabla %img_",
          1 => "More text %img_ blabla %img_"
      );
      
      $img = array("BLACK","GREEN","BLUE", "RED", "PINK");
      
      foreach ($text as &$row) {
              $row = str_replace("%img_", "%%img_%s", $row);
              $row = vsprintf($row, $img);
      }
      
      print_r($text);
      

      哪些输出:

      Array
      (
          [0] => Bunch of text %img_BLACK %img_GREEN: Some more text blabla %img_BLUE
          [1] => More text %img_BLACK blabla %img_GREEN
      )
      

      【讨论】:

        【解决方案4】:

        另一个使用anonymous function 和一些spl 的php 5.3+ 版本:

        $text = array(
          "Bunch of text %img_ %img_: Some more text blabla %img_",
          "More text %img_ blabla %img_"
        );
        $img = new ArrayIterator(array("BLACK","GREEN","BLUE", "RED", "PINK"));
        foreach($text as &$t) {
          $t = preg_replace_callback('/%img_/', function($s) use($img) {
              $rv = '%img_' . $img->current();
              $img->next();
              return $rv;
            }, $t);
        }
        
        var_dump($text);
        

        【讨论】:

        • 这类似于我最初解决问题的方式。感谢这个 VolkerK。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-02-03
        • 1970-01-01
        • 2016-04-20
        • 2015-05-23
        • 1970-01-01
        • 2021-04-09
        • 1970-01-01
        相关资源
        最近更新 更多