[文章目录]显示/隐藏

忙里偷闲
先唠叨几句,
已经好久没更新了
手机弄Emlog后台模板,弄的我眼睛疲劳,
这次弄完就不打算弄后台模板了,
我自己,已经够用了,
不折腾了后台了,太累了,
就在这两天发布,大家请关注吧!
上次群里的人说要这个功能,现在我就发布了
好了言归正传!
我说的文章内容功能三合一就是
1.文章内容添加文章索引,我这里是调用H2的
2.文章内容有含有标签的添加链接
3.文章内容有外链的添加nofollow

关键代码添加到Module.php文件中

 
  1. <?php  
  2. function article_index($content , $domain) {  
  3.             global $CACHE;  
  4.             $tag_cache = $CACHE->readCache('tags');  
  5.             $matches = array();  
  6.             $ul_li = '';  
  7.             $r = "/<h2>([^<]+)<\/h2>/im";  
  8.     if(preg_match_all($r,$content,$matches)) {  
  9.            foreach($matches[1] as $num => $title) {  
  10.            $content = str_replace($matches[0][$num], '<h2 id="title-'.$num.'">'.$title.'</h2>', $content);  
  11.            $ul_li .= '<li><a href="#title-'.$num.'" title="'.$title.'">'.$title."</a></li>\n";  
  12.              }  
  13.  $content = "\n<div id=\"article-index\"> 
  14.  <b>[文章目录]</b> 
  15.  <ul id=\"index-ul\">\n" . $ul_li . "</ul> 
  16.  </div>\n" . $content;  
  17.  }  
  18.  foreach($tag_cache as $value){  
  19.                 $tag_url = Url::tag($value['tagurl']);  
  20.                 $keyword = $value['tagname'];  
  21.                 $cleankeyword = stripslashes($keyword);  
  22.                 $url = "<a href=\"{$tag_url}\" title=\"浏览关于“{$cleankeyword}”的文章\" target=\"_blank\" >{$cleankeyword}</a>";  
  23.                 $regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s';  
  24.                 $content = preg_replace($regEx,$url,$content);          
  25. }  
  26. preg_match_all('/href="(.*?)"/', $log_content$matches);  
  27.     if ($matches) {  
  28.         foreach ($matches[1] as $val) {  
  29.             if (strpos($val$domain) === false) {  
  30.                 $log_content = str_replace('href="' . $val . '"', 'href="' . $val . '" rel="external nofollow" ', $log_content);  
  31.             }  
  32.         }  
  33.     }  
  34. preg_match_all('/src="(.*?)"/', $log_content$matches);  
  35.     if ($matches) {  
  36.         foreach ($matches[1] as $val) {  
  37.             if (strpos($val$domain) === false) {  
  38.                 $log_content = str_replace('src="' . $val . '"', 'src="' . $val . '" rel="external nofollow" ', $log_content);  
  39.             }  
  40.         }  
  41.     }  
  42. return $content;  
  43. }  
  44. ?>  

添加Css样式

 
  1.  #article-index {  
  2.  border1px dashed #dedfe1;  
  3.  floatleft;  
  4.  margin: 0 20px 20px 0;  
  5.  padding: 0 6px;  
  6.  width280px;  
  7.  line-height24px;  
  8. }  
  9. #article-index b {  
  10.  border-bottom1px dashed #ddd;  
  11.  displayblock;  
  12.  line-height30px;  
  13.  padding: 0 4px;  
  14. }  
  15. #index-ul {  
  16.  margin5px;  
  17.  line-height: 1.8;  
  18. }  
  19. #index-ul li {  
  20.  backgroundnone repeat scroll 0 0 transparent;  
  21.  list-style-typedecimal;  
  22.  padding4px 10px;  
  23.  border-left1px solid #e2e2e2;  
  24.  border-bottom1px solid #e2e2e2;  
  25. }  
  26. #index-ul li {  
  27.  list-stylecircle outside none;  
  28. }  

最后,打开echo_log.php文件

查找

 
  1. <?php echo $log_content; ?>  

替换(当然你们自己改过,就不是找这个咯)

 
  1. <?php echo article_index($log_content);?>  

完工,祝大家好运,转载请著名地方哦,手机码字不容易啊