2009-10-17 36 views
3

由於某種原因,我正在爲此苦苦掙扎。用第二個數組中的值替換第一個數組中的字符串

我有以下2個數組,我需要從$ IMG陣列取數組值,爲了將它們插入到$文本數組,附加/更換%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 = array(
    0 => "Bunch of text %img_BLACK %img_GREEN: Some moretext blabla %img_BLUE", 
    1 => "More text %img_RED blabla %img_PINK" 
); 

注:$ IMG數組中的項目數會有所不同,但將永遠是一樣的%IMG_在$文本數陣列。

回答

5

這裏有一種方法,你可以做到這一點,利用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); 
+0

很高興看到一些OO解決方案在這裏:) – Mark

+0

啊,謝謝保羅。做得很好!爲我保存了一堆頭部劃痕。 – k00k

1

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); 

如果你喜歡,你可以將它分組到一個函數。

希望這會有所幫助。

+0

也很有用的NawaMan,謝謝。 – k00k

+0

請注意,從PHP 5.3開始,[split()'](http://php.net/split)已被棄用。 – 2012-01-31 18:29:46

1

這裏的另一種方式:

<?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 
) 
2

另一個版本的PHP 5.3+使用anonymous function和一些spl

$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); 
+0

這與我最初攻擊該問題的方式類似。感謝這個VolkerK。 – k00k

相關問題