将多次出现的字符串替换为不同的值
我有一个生成包含某些令牌的内容的脚本,我需要用单独循环产生的不同内容替换每个出现的令牌。
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为您无法使用任何搜索和替换功能来执行此操作,因此您必须自己编写替换代码。
在我看来,这个问题与
explode()
配合得很好。因此,使用您提供的示例令牌生成器,解决方案如下所示: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:我知道这是一个旧线程,但我在尝试实现类似的目标时偶然发现了它。如果其他人看到这个,我认为这更好一点:
创建一些示例文本:
使用正则表达式查找搜索字符串:
定义一个回调函数来更改匹配
回显结果:
因此作为函数集:
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:
Find the search string with regex:
Define a callback function to change the matches
Echo the results:
So as a function set:
我遇到了类似的问题,我有一个需要读取的文件。它有多次出现的标记,我需要用数组中的不同值替换每次出现的情况。
该函数将替换在“haystack”中找到的每个出现的“token”/“needle”,并将其替换为索引数组中的值。
顺便说一句,“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.
By the way, 'mostr_replace' is short for "Multiple Occurrence String Replace".
您可以使用以下代码:
You can use the following code: