怎样去获取文章简介

来源:互联网 发布:淘宝打印电子面单步骤 编辑:程序博客网 时间:2024/04/18 12:52
相信大家在做博客,文章管理系统之类的时候经常会遇到这样的问题:
 
   把一些文章抽出来放在首页,当然这些文章要显示内容简介。但是问题来了,这些内容简介有时候有HTML,有时候没有,如果单纯的去截断字符似乎有点笨拙,特别是在DIV页面上,很容易就截断了HTML,导致页面变形了。
    解决办法有很多,有些人是用div的样式去隐藏来解决,但是还是不完整,导致页面变形,或者直接显示出整篇文章来。

下面的代码就可以解决这个问题,(:) 代码很简单,只是介乎你有没有认真去想而已了,在这里就不对代码做相信说明了。)
使用方法:直接调用 StripLongContent("你的内容",你要显示在页面上的长度);

static Regex Content_regex = new Regex("<[^<>]+>?", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline);
        
public static string StripLongContent(string content, int length)
        
{
            
//去掉JS
            
//content = StripScriptTags(content);
            string input = "";
            
//如果总长度都不够指定的长度,直接返回
            if (content.Length <= length)
                
return content;
            
//匹配出<>中的标签 
            Match mx = Content_regex.Match(content, 0, content.Length);
            Stack tagStack 
= new Stack();
            
int startIndex = 0;
            
string temp = "";
            
int maxlength = 0;
            
//其实很简单,保证截取出来的段落有结束的html标签就可以了,但是也不能出现类似
            
//<tr><td></tr>
            
//的HTML问题
            while (mx.Value != string.Empty)
            
{
                temp 
= content.Substring(startIndex, mx.Index);
                
//当然也要保证非显示字符数不要包括到要显示的字符数中。否则会严重影响简介的质量
                
//或者你要这里确定一个br算多少个字符都可以
                if (maxlength + temp.Length >= length)
                
{
                    temp 
= temp.Substring(0, length - maxlength);
                    input 
= input + temp + ""
                    maxlength 
= length;
                    
break;
                }

                maxlength 
+= temp.Length;
                input 
= input + temp + mx.Value;

                
//分别对HTML标签进行压栈出栈操作
                if (mx.Value.EndsWith("/>"))
                
{
                }

                
else if (mx.Value.StartsWith("</"))
                    tagStack.Pop();
                
else
                    tagStack.Push(mx.Value);



                
int index = content.IndexOf(mx.Value);
                content 
= content.Remove(0, index);
                content 
= content.Remove(0, mx.Length);

                mx 
= Content_regex.Match(content, 0, content.Length);
            }

            
//如果整篇文章没有HTML标签,直接切就可以了
            if (maxlength == 0)
            
{
                
if (content.Length < length)
                    content 
= input + content;
                
else
                    content 
= input + content.Substring(0, length) + "";
                
return content;
            }

            
//按顺序补全未结束的HTML标签
            while (tagStack.Count > 0)
            
{
                
string tag = tagStack.Pop().ToString();
                
if (tag.IndexOf(' '> 0)
                    tag 
= tag.Substring(0, tag.IndexOf(' ')).Replace("<""");
                
else
                    tag 
= tag.Replace("<""").Replace(">""");
                input 
= input + "</" + tag + ">";
            }

            
return input;
        }
 
原创粉丝点击