文本中的多个简码
Multiple shortcodes within a text
我有以下文字
this is line one
[gallery]
this is line two
[gallery]
this is line three
我在php
中也有如下代码
$text = 'this is line one [gallery] this is line two [gallery] this is line three';
$gallery = array('gallery name1', 'gallery name2');
foreach ($gallery as $key => $val) {
$text = preg_replace('#\[gallery\]#si', $val, $text);
}
我想用数组 $gallery 的第一个值替换第一个 [gallery],用数组 $gallery 的第二个值替换第二个 [gallery]。
我该怎么做?
您需要添加一个限制,以便只有 1 个条目被更改:
$text = preg_replace('#\[gallery\]#si', $val, $text, 1);
^ add a limit
现在,在每次迭代中,[gallery]
的 1 个实例将被替换。
我有以下文字
this is line one
[gallery]
this is line two
[gallery]
this is line three
我在php
中也有如下代码$text = 'this is line one [gallery] this is line two [gallery] this is line three';
$gallery = array('gallery name1', 'gallery name2');
foreach ($gallery as $key => $val) {
$text = preg_replace('#\[gallery\]#si', $val, $text);
}
我想用数组 $gallery 的第一个值替换第一个 [gallery],用数组 $gallery 的第二个值替换第二个 [gallery]。
我该怎么做?
您需要添加一个限制,以便只有 1 个条目被更改:
$text = preg_replace('#\[gallery\]#si', $val, $text, 1);
^ add a limit
现在,在每次迭代中,[gallery]
的 1 个实例将被替换。