2011-09-25 49 views
3

我有一個腳本可以生成包含某些標記的內容,我需要用一個單獨的循環來替換每個出現的標記。使用不同的值替換多次出現的字符串

它使用簡單,str_replace函數與相同內容替換令牌的所有事件,但我需要與循環的下一個結果替換每次出現。

我沒有看到這樣的回答:Search and replace multiple values with multiple/different values in PHP5?

但它是從預先定義的數組,我沒有工作。

示例內容:

This is an example of %%token%% that might contain multiple instances of a particular 
%%token%%, that need to each be replaced with a different piece of %%token%% generated 
elsewhere. 

我需要更換的每次出現%%令牌%%的內容產生的,爲了便於討論,通過這種簡單的循環:

for($i=0;$i<3;$i++){ 
    $token = rand(100,10000); 
} 

所以替換每個% %token %%與一個不同的隨機數值$ token。

這是一些簡單的,我只是沒有看到?

謝謝!

+0

我不認爲有一個庫函數爲你做這個。您只需編碼搜索並替換自己。 –

+0

我知道這一點,我正在尋求幫助。我一直無法圍繞解決方案來解決這個問題。我的preg_replace技能不是很好,但我發現了一些看起來很有用的東西,但我無法弄清楚如何修改它以滿足我的需求: $ string ='某個foo的時間,因爲它是我們想要的foo和我們會得到。因爲它是我們想要的,我們會得到的foo。 $ replaceme ='foo'; $ replacewith ='bar'; $ nthtimes = 2; echo preg_replace(「/ ((.*?))(".$ replaceme。」)){「。$ nthtimes。」}/e「,'(preg_replace(」/".$ replaceme。「$ /」, 「」,「\ 0」))。$ replacewith',$ string); –

+0

對不起,誤解了這個問題。見答案。 –

回答

4

我不認爲你可以使用任何搜索和替換功能來做到這一點,所以你必須自己編碼替換。

在我看來,這個問題很適合explode()。所以,使用你提供的示例令牌生成器,該解決方案是這樣的:

$shrapnel = explode('%%token%%', $str); 
$newStr = ''; 
for ($i = 0; $i < count($shrapnel); ++$i) { 
    // The last piece of the string has no token after it, so we special-case it 
    if ($i == count($shrapnel) - 1) 
     $newStr .= $shrapnel[$i]; 
    else 
     $newStr .= $shrapnel[$i] . rand(100,10000); 
} 
+0

是的,這是關於我親自提出的最接近的解決方案,但我真的希望也許可以用某種preg_replace wizardry做某種形式的事。畢竟,我可能只需要走爆炸路線。讓我玩這個,看看我能否做到這一點。非常感謝幫助! –

+0

您可以使用preg_match代替爆炸,但結果會更復雜。 :/ –

+0

好熱的該死的......這個解決方案完全脫離了大門。之前我曾想到過這個基本方法,但由於某種原因,認爲這樣處理會是一團糟。我應該以我的直覺走了。 =)感謝您的幫助! –

0

您可以使用下面的代碼:

$content = "This is an example of %%token%% that might contain multiple instances of a particular %%token%%, that need to each be replaced with a different piece of %%token%% generated elsewhere."; 

while (true) 
{ 
    $needle = "%%token%%"; 
    $pos = strpos($content, $needle); 
    $token = rand(100, 10000); 
    if ($pos === false) 
    { 
     break; 
    } 
    else 
    { 
     $content = substr($content, 0, 
          $pos).$token.substr($content, $pos + strlen($token) + 1); 
    } 
} 
1

我知道這是一個古老的線程,但我碰到它絆倒同時試圖實現類似的東西。如果任何人看到這個,我認爲這是一個更好一點:

創建一些示例文本:

$text="This is an example of %%token%% that might contain multiple instances of a particular 
%%token%%, that need to each be replaced with a different piece of %%token%% generated 
elsewhere."; 

查找與正則表達式搜索字符串:

$new_text = preg_replace_callback("|%%token%%|", "_rand_preg_call", $text); 

定義一個回調函數來改變比賽

function _rand_preg_call($matches){ 
    return rand(100,10000); 
} 

回聲結果:

echo $new_text; 

所以,作爲一個功能集:

function _preg_replace_rand($text,$pattern){ 
    return preg_replace_callback("|$pattern|", "_rand_preg_call", $text); 
} 

function _rand_preg_call($matches){ 
    return rand(100,10000); 
} 
1

我有一個類似的問題,我不得不說,我需要讀取的文件。它有多次出現的令牌,我需要用數組中的不同值來替換每個出現的次數。

該函數將替換在「乾草堆」中找到的每個「token」/「needle」,並將其替換爲索引數組中的值。

function mostr_replace($needle, $haystack, $replacementArray, $needle_position = 0, $offset = 0) 
{ 
    $counter = 0; 
    while (substr_count($haystack, $needle)) { 

     $needle_position = strpos($haystack, $needle, $offset); 
     if ($needle_position + strlen($needle) > strlen($haystack)) { 
      break; 
     } 
     $haystack = substr_replace($haystack, $replacementArray[$counter], $needle_position, strlen($needle)); 
     $offset = $needle_position + strlen($needle); 
     $counter++; 
    } 

    return $haystack; 
} 

順便說一下,'mostr_replace'是「多次發生字符串替換」的縮寫。

+0

我會建議一個改進,找到一次計數,只是通過字符串運行。每次運行substr_count有點太昂貴。 – v010dya

相關問題