我有一个脚本可以生成包含某些标记的内容,我需要用一个单独的循环来替换每个出现的标记。使用不同的值替换多次出现的字符串
它使用简单,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。
这是一些简单的,我只是没有看到?
谢谢!
我不认为有一个库函数为你做这个。您只需编码搜索并替换自己。 –
我知道这一点,我正在寻求帮助。我一直无法围绕解决方案来解决这个问题。我的preg_replace技能不是很好,但我发现了一些看起来很有用的东西,但我无法弄清楚如何修改它以满足我的需求: $ string ='某个foo的时间,因为它是我们想要的foo和我们会得到。因为它是我们想要的,我们会得到的foo。 $ replaceme ='foo'; $ replacewith ='bar'; $ nthtimes = 2; echo preg_replace(“/ ((.*?))(".$ replaceme。”)){“。$ nthtimes。”}/e“,'(preg_replace(”/".$ replaceme。“$ /”, “”,“\ 0”))。$ replacewith',$ string); –
对不起,误解了这个问题。见答案。 –