
CSS样式<style>#article-index{-moz-border-radius:6px 6px 6px 6px;border:1px solid #DEDFE1;float:right;margin:0 0 15px 15px;padding:0 6px;width:200px;line-height:23px;}#article-index strong{border-bottom:1px dashed #DDDDDD;display:block;line-height:30px;padding:0 4px;}#index-ul{margin:0;padding-top:10px;padding-bottom:10px;padding-inline-start:5px;}#index-ul li{background:none repeat scroll 0 0 transparent;list-style-type:disc;padding:0;margin-left:20px;}</style>
php函数function tables_of_content($content) {
$matches = array();
$ul_li = '';
$r = '/<h([2-6]).*?\>(.*?)<\/h[2-6]>/is';
if(preg_match_all($r, $content, $matches)) {
foreach($matches[1] as $key => $value) {
$title = trim(strip_tags($matches[2][$key]));
$content = str_replace($matches[0][$key], '<h' . $value . ' id="title-' . $key . '">'.$title.'</h2>', $content);
$ul_li .= '<li><a href="#title-'.$key.'" title="'.$title.'">'.$title."</a></li>\n";
}
$content = "\n<div id=\"article-index\">
<strong>文章目录</strong>
<ol id=\"index-ul\">\n" . $ul_li . "</ol>
</div>\n" . $content;
}
return $content;
}
实现方式
function tables_of_content($content) { $matches = array(); $ul_li = ''; $r = '/<h([2-6]).*?\>(.*?)<\/h[2-6]>/is'; if(preg_match_all($r, $content, $matches)) { foreach($matches[1] as $key => $value) { $title = trim(strip_tags($matches[2][$key])); $content = str_replace($matches[0][$key], '<h' . $value . ' id="title-' . $key . '">'.$title.'</h2>', $content); $ul_li .= '<li><a href="#title-'.$key.'" title="'.$title.'">'.$title."</a></li>\n"; } $content = "\n<div id=\"article-index\"> <strong>文章目录</strong> <ol id=\"index-ul\">\n" . $ul_li . "</ol> </div>\n" . $content; } return $content; }
实现方式
只要找到PHP程序里的内容变量,如 message、post、body 等类似变量,加上tables_of_content 函数,主标题用<h*>(*从2-6),然后把css 样式加入到样式表就可以实现目录查看。
上图是本站的演示。
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。
评论