{pboot:foreach str={label:hotkey} char=|}
[foreach:name]
{/pboot:foreach}
修改步驟
$content=$this->parserForeachLabel($content);//指定隨意內(nèi)容按條件遍歷
// 解析指定隨意內(nèi)容遍歷,支持設(shè)定分隔符
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 ++) {
// 獲取調(diào)節(jié)參數(shù)
$params = $this->parserParam($matches[1][$i]);
$str = '';
$char = ',';
if (! self::checkLabelLevel($params)) {
$content = str_replace($matches[0][$i], '', $content);
continue;
}
// 分離參數(shù)
foreach ($params as $key => $value) {
switch ($key) {
case 'num':
$num = $value;
break;
case 'str':
$str = $value;
break;
case 'char':
if ($value) $char = $value;
break;
}
}
// 無(wú)數(shù)據(jù)直接替換為空并跳過(guò)
if (! $str) {
$content = str_replace($matches[0][$i], '', $content);
continue;
}
// 匹配到內(nèi)部標(biāo)簽
if (preg_match_all($pattern2, $matches[2][$i], $matches2)) {
$count2 = count($matches2[0]); // 循環(huán)內(nèi)的內(nèi)容標(biāo)簽數(shù)量
} else {
$count2 = 0;
}
$out_html = '';
$key = 1;
$arr = explode($char,$str);
foreach ($arr as $value) { // 按查詢圖片條數(shù)循環(huán)
$one_html = $matches[2][$i];
for ($j = 0; $j < $count2; $j ++) { // 循環(huán)替換數(shù)據(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;
}
調(diào)用標(biāo)簽
{pboot:foreach str={label:hotkey}}
[foreach:name]
{/pboot:foreach}
2、其他分割符合,使用char參數(shù),如:|
{pboot:foreach str={label:hotkey} char=|}
[foreach:name]
{/pboot:foreach}
注意事項(xiàng)版權(quán)聲明: 本站資源均來(lái)自互聯(lián)網(wǎng)或會(huì)員發(fā)布,如果侵犯了您的權(quán)益請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)刪除!謝謝!
轉(zhuǎn)載請(qǐng)注明: pbootcms增加foreach指定內(nèi)容按條件遍歷(默認(rèn)逗號(hào)條件)方法