{pboot:foreach str={label:hotkey} char=|}
[foreach:name]
{/pboot:foreach}
修改步骤
$content=$this->parserForeachLabel($content);//指定随意内容按条件遍历
// 解析指定随意内容遍历,支持设定分隔符
public function parserForeachLabel($content)
{
$pattern = '/{pboot:foreach(s+[^}]+)?}([sS]*?){/pboot:foreach}/';
$pattern2 = '/[foreach:([w]+)(s+[^]]+)?]/';
if (preg_match_all($pattern, $content, $matches)) {
$count = count($matches[0]);
for ($i = 0; $i < $count; $i ++) {
// 获取调节参数
$params = $this->parserParam($matches[1][$i]);
$str = '';
$char = ',';
if (! self::checkLabelLevel($params)) {
$content = str_replace($matches[0][$i], '', $content);
continue;
}
// 分离参数
foreach ($params as $key => $value) {
switch ($key) {
case 'num':
$num = $value;
break;
case 'str':
$str = $value;
break;
case 'char':
if ($value) $char = $value;
break;
}
}
// 无数据直接替换为空并跳过
if (! $str) {
$content = str_replace($matches[0][$i], '', $content);
continue;
}
// 匹配到内部标签
if (preg_match_all($pattern2, $matches[2][$i], $matches2)) {
$count2 = count($matches2[0]); // 循环内的内容标签数量
} else {
$count2 = 0;
}
$out_html = '';
$key = 1;
$arr = explode($char,$str);
foreach ($arr as $value) { // 按查询图片条数循环
$one_html = $matches[2][$i];
for ($j = 0; $j < $count2; $j ++) { // 循环替换数据
$params = $this->parserParam($matches2[2][$j]);
switch ($matches2[1][$j]) {
case 'n':
$one_html = str_replace($matches2[0][$j], $this->adjustLabelData($params, $key) - 1, $one_html);
break;
case 'i':
$one_html = str_replace($matches2[0][$j], $this->adjustLabelData($params, $key), $one_html);
break;
case 'name':
$one_html = str_replace($matches2[0][$j], $this->adjustLabelData($params, $value), $one_html);
break;
}
}
$key ++;
$out_html .= $one_html;
if (isset($num) && $key > $num) {
unset($num);
break;
}
}
$content = str_replace($matches[0][$i], $out_html, $content);
}
}
return $content;
}
调用标签
{pboot:foreach str={label:hotkey}}
[foreach:name]
{/pboot:foreach}
2、其他分割符合,使用char参数,如:|
{pboot:foreach str={label:hotkey} char=|}
[foreach:name]
{/pboot:foreach}
注意事项版权声明: 本站资源均来自互联网或会员发布,如果侵犯了您的权益请与我们联系,我们将在24小时内删除!谢谢!