[文章目录]显示/隐藏
忙里偷闲
先唠叨几句,
已经好久没更新了
手机弄Emlog后台模板,弄的我眼睛疲劳,
这次弄完就不打算弄后台模板了,
我自己,已经够用了,
不折腾了后台了,太累了,
就在这两天发布,大家请关注吧!
上次群里的人说要这个功能,现在我就发布了
好了言归正传!
我说的文章内容功能三合一就是
1.文章内容添加文章索引,我这里是调用H2的
2.文章内容有含有标签的添加链接
3.文章内容有外链的添加nofollow
关键代码添加到Module.php文件中
- <?php
- function article_index($content , $domain) {
- global $CACHE;
- $tag_cache = $CACHE->readCache('tags');
- $matches = array();
- $ul_li = '';
- $r = "/<h2>([^<]+)<\/h2>/im";
- if(preg_match_all($r,$content,$matches)) {
- foreach($matches[1] as $num => $title) {
- $content = str_replace($matches[0][$num], '<h2 id="title-'.$num.'">'.$title.'</h2>', $content);
- $ul_li .= '<li><a href="#title-'.$num.'" title="'.$title.'">'.$title."</a></li>\n";
- }
- $content = "\n<div id=\"article-index\">
- <b>[文章目录]</b>
- <ul id=\"index-ul\">\n" . $ul_li . "</ul>
- </div>\n" . $content;
- }
- foreach($tag_cache as $value){
- $tag_url = Url::tag($value['tagurl']);
- $keyword = $value['tagname'];
- $cleankeyword = stripslashes($keyword);
- $url = "<a href=\"{$tag_url}\" title=\"浏览关于“{$cleankeyword}”的文章\" target=\"_blank\" >{$cleankeyword}</a>";
- $regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s';
- $content = preg_replace($regEx,$url,$content);
- }
- preg_match_all('/href="(.*?)"/', $log_content, $matches);
- if ($matches) {
- foreach ($matches[1] as $val) {
- if (strpos($val, $domain) === false) {
- $log_content = str_replace('href="' . $val . '"', 'href="' . $val . '" rel="external nofollow" ', $log_content);
- }
- }
- }
- preg_match_all('/src="(.*?)"/', $log_content, $matches);
- if ($matches) {
- foreach ($matches[1] as $val) {
- if (strpos($val, $domain) === false) {
- $log_content = str_replace('src="' . $val . '"', 'src="' . $val . '" rel="external nofollow" ', $log_content);
- }
- }
- }
- return $content;
- }
- ?>
添加Css样式
- #article-index {
- border: 1px dashed #dedfe1;
- float: left;
- margin: 0 20px 20px 0;
- padding: 0 6px;
- width: 280px;
- line-height: 24px;
- }
- #article-index b {
- border-bottom: 1px dashed #ddd;
- display: block;
- line-height: 30px;
- padding: 0 4px;
- }
- #index-ul {
- margin: 5px;
- line-height: 1.8;
- }
- #index-ul li {
- background: none repeat scroll 0 0 transparent;
- list-style-type: decimal;
- padding: 4px 10px;
- border-left: 1px solid #e2e2e2;
- border-bottom: 1px solid #e2e2e2;
- }
- #index-ul li {
- list-style: circle outside none;
- }
最后,打开echo_log.php文件
查找
- <?php echo $log_content; ?>
替换(当然你们自己改过,就不是找这个咯)
- <?php echo article_index($log_content);?>
完工,祝大家好运,转载请著名地方哦,手机码字不容易啊
感谢楼主分享
老司机就是老司机,不同凡响,很实用
好多代码
老哥这个很实用!要是可以修正一下,比如代码片段里面的不要加上tag的链接就完美了
@Mrxn:代码片段,还真没考虑到!你不说还真没注意!
@老司机:嗯 可以考虑遇见pre标签内的不执行替换, 我才发现另外一个不算bug的bug,比如文章中有个链接 http://xxx.com/info.php,而系统恰好有PHP标签和info标签的话,就会很尴尬.因为内链替换会把链接加上标签链接...不过这个不好解决(我认为...或许正册匹配到是URL的不替换?) 其实也可以不用解决 也可以当成一种挑战 试试 -_- 嘿嘿 反正pre标签解决了就基本上完美了
@Mrxn:嗯,有空在折腾这功能,不急!而且我的的代码高亮,不好弄!