织梦网站用php正则给织梦文章加导航目录

通过正则表达式,自动文章内容提取带有H2标签的文字为目录索引

$str = '<h2 class="archt"> <strong>我是里面的内容</strong></h2><h2 class="archt2">我是第2个内容</h2><strong>我没有h2包在里面</strong>'; preg_match_all('/<h2.*?>.*?(<[^>]+>)?([^<]+)(<\/[^>]+>)?<\/h2>/s', $str, $arr); print_r($arr); Array ( [0] => Array ( [0] => <h2 class="archt"> <strong>我是里面的内容</strong></h2> [1] => <h2 class="archt2">我是第2个内容</h2> ) [1] => Array ( [0] => <strong> [1] => ) [2] => Array ( [0] => 我是里面的内容 [1] => 我是第2个内容 ) [3] => Array ( [0] => </strong> [1] => ) ) function article_index($content) { $matches = array(); $ul_li = ''; $r = '/<h2.*?>.*?(<[^>]+>)?([^<]+)(<\/[^>]+>)?<\/h2>/s'; if(preg_match_all($r, $content, $matches)) { foreach($matches[2] as $num => $title) { $ul_li .= '<em>'.$title."&nbsp;</em>"; } } return $ul_li; }  

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/ac29ac2e74694337aa4e27b8f74a3246.html