【问题标题】:Replace multiple occurrences of a string with different values用不同的值替换多次出现的字符串
【发布时间】:2011-11-24 12:31:35
【问题描述】:

我有一个生成包含某些标记的内容的脚本,我需要用一个单独的循环产生的不同内容替换每次出现的标记。

使用 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.

为了论证,我需要通过这个简单的循环将每次出现的 %%token%% 替换为生成的内容:

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

因此将每个 %%token%% 替换为不同的随机数值 $token。

这是我没有看到的简单东西吗?

谢谢!

【问题讨论】:

  • 我认为没有库函数可以为您执行此操作。您只需编写搜索代码并自己替换即可。
  • 我知道,我正在寻求帮助。我一直无法解决这个问题。我的 preg_replace 技能不是很好,但我发现了一些看起来很有帮助的东西,但我不知道如何根据需要修改它: $string = 'Time for some foo, because it is foo we want and foo我们将得到。是时候来点 foo 了,因为我们想要的是 foo,我们会得到 foo。'; $replaceme = 'foo'; $replacewith = '酒吧'; $n次 = 2; echo preg_replace("/((.*?)(".$replaceme.")){".$nthtimes."}/e", '(preg_replace("/".$replaceme."$/", "" , "\0")).$replacewith', $string);
  • 抱歉,误解了这个问题。见答案。

标签: php string replace str-replace


【解决方案1】:

我有一个类似的问题,我有一个需要阅读的文件。它有多次出现的标记,我需要用数组中的不同值替换每次出现。

此函数将替换在“haystack”中找到的每次出现的“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' 是“Multiple Occurrence String Replace”的缩写。

【讨论】:

  • 我建议进行改进,找到一次计数,然后遍历字符串。 substr_count 每次都运行它有点太贵了。
【解决方案2】:

我知道这是一个旧线程,但我在尝试实现类似的目标时偶然发现了它。如果其他人看到这个,我认为这更好一点:

创建一些示例文本:

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

【讨论】:

    【解决方案3】:

    您可以使用以下代码:

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

    【讨论】:

    • 有帮助但还不够: $content = substr($content, 0,$pos) 。 $令牌。 substr($content, $pos + strlen($needle));
    【解决方案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);
    }
    

    【讨论】:

    • 是的,这是我个人提出的最接近的解决方案,尽管我真的希望它可以通过某种 preg_replace 魔法来完成。也许我最终只需要走爆炸路线。让我玩这个,看看我能不能让它完成这项工作。非常感谢帮助!
    • 你可以用 preg_match 代替explode,但结果会更复杂。 ://
    • 好吧,该死的......这个解决方案非常完美。我之前曾考虑过这种基本方法,但出于某种原因认为这样处理会一团糟。我应该凭直觉走。 =) 谢谢你的帮助!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多