一、解析列表
1、效果图
2、示例代码
<?php include_once 'parse.func.php'; $data = array( array( 'scode' => 2, 'link' => 'https://www.baidu.com', 'title' => '123', 'date' => '2023-02-16 13:47:20' ) ); $content = <<<HTML <html> <head><title>标签解析</title></head> <body> <ul> {yang:list scode=2 num=3} <li class="wow fadeInUp" data-wow-delay="0.[list:i]s"> <a href="[list:link]"> <article> <div class="item-date fs16">[list:date style=Y-m-d]</div> <div class="item-title"> <h1 class="fs26">[list:title]</h1> </div> <div class="item-more"> <div class="item-round"></div> <div class="item-line__box"> <div class="item-line"></div> </div> <span>more</span> </div> </article> </a> </li> {/yang:list} </ul> </body> HTML; $pattern = '/\{yang:list(\s+[^}]+)?\}([\s\S]*?)\{\/yang:list\}/'; $pattern2 = '/\[list:([\w\+\-\*\/\%]+)(\s+[^]]+)?\]/'; if (preg_match_all($pattern, $content, $matches)) { //var_dump($matches); $count = count($matches[0]); for ($i = 0; $i < $count; $i ++) { // 获取调节参数 scode=2 num=3 $params = parserParam($matches[1][$i]); //根据参数从数据库获取数据。。。 //这里的数据用$data数据模拟 // 无数据直接替换 if (! $data) { $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; // 按查询的数据条数循环 foreach ($data as $value) { $one_html = $matches[2][$i]; // 循环替换数据 for ($j = 0; $j < $count2; $j ++) { //获取参数,如:[list:date style=Y-m-d]中的style $params = parserParam($matches2[2][$j]); //替换列表中的标签,如:[list:title] $one_html = parserList($matches2[1][$j], $matches2[0][$j], $one_html, $value, $params, $key); } $key ++; $out_html .= $one_html; } $content = str_replace($matches[0][$i], $out_html, $content); } } echo $content; ?>
标签:count,自定义,matches,list,content,html,matches2,PHP,模板 From: https://www.cnblogs.com/yang-2018/p/17126951.html