将多次出现的字符串替换为不同的值

发布于 2024-12-06 09:01:19 字数 762 浏览 1 评论 0原文

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

使用 str_replace 将所有出现的标记替换为相同的内容很简单,但我需要将每个出现的位置替换为循环的下一个结果。

我确实看到了这个答案: 搜索并替换多个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%%。

这是我没有看到的简单事情吗?

谢谢!

I have a script that generates content containing certain tokens, and I need to replace each occurrence of a token, with different content resulting from a separate loop.

It's simple to use str_replace to replace all occurrences of the token with the same content, but I need to replace each occurrence with the next result of the loop.

I did see this answer: Search and replace multiple values with multiple/different values in PHP5?

however it is working from pre-defined arrays, which I don't have.

Sample 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.

I need to replace each occurrence of %%token%% with content generated, for argument's sake, by this simple loop:

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

So replace each %%token%% with a different random number value $token.

Is this something simple that I'm just not seeing?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吻安 2024-12-13 09:01:19

我认为您无法使用任何搜索和替换功能来执行此操作,因此您必须自己编写替换代码。

在我看来,这个问题与 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);
}

I don't think you can do this using any of the search and replace functions, so you'll have to code up the replace yourself.

It looks to me like this problem works well with explode(). So, using the example token generator you provided, the solution looks like this:

$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);
}
自我难过 2024-12-13 09:01:19

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

创建一些示例文本:

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

I know this is an old thread, but I stumbled across it while trying to achieve something similar. If anyone else sees this, I think this is a little nicer:

Create some sample text:

$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.";

Find the search string with regex:

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

Define a callback function to change the matches

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

Echo the results:

echo $new_text;

So as a function set:

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

function _rand_preg_call($matches){
  return rand(100,10000);
}
北方的韩爷 2024-12-13 09:01:19

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

该函数将替换在“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”是“多次出现字符串替换”的缩写。

I had a similar issue where I had a file that I needed to read. It had multiple occurrences of a token, and I needed to replace each occurrence with a different value from an array.

This function will replace each occurrence of the "token"/"needle" found in the "haystack" and will replace it with a value from an indexed array.

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

By the way, 'mostr_replace' is short for "Multiple Occurrence String Replace".

女中豪杰 2024-12-13 09:01:19

您可以使用以下代码:

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

You can use the following code:

$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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文