2011-09-25 161 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