Php 文本中的多个短代码
我有以下文字 这是一号线 [画廊] 这是二号线 [画廊] 这是三号线 我还有以下php代码Php 文本中的多个短代码,php,shortcode,Php,Shortcode,我有以下文字 这是一号线 [画廊] 这是二号线 [画廊] 这是三号线 我还有以下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('#
$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]
如何执行此操作?您需要添加一个限制,以便只更改一个条目:
$text = preg_replace('#\[gallery\]#si', $val, $text, 1);
^ add a limit
现在,在每一次迭代中,将替换一个
[gallery]
实例。请拿起并阅读指南。因此,它不是一个代码编写服务。我们希望您向我们展示您当前编写的代码(即使是尝试),并展示一些研究成果。使用preg_replace我已经更新了我的问题。