怎样去获取文章简介

   相信大家在做博客,文章管理系统之类的时候经常会遇到这样的问题:
 
   把一些文章抽出来放在首页,当然这些文章要显示内容简介。但是问题来了,这些内容简介有时候有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标签就可以了
            
while (mx.Value != string.Empty)
            
{
                temp 
= content.Substring(startIndex, mx.Index);
                //当然也要保证非显示字符数不要包括到要显示的字符数中。否则会严重影响简介的质量
                
if (maxlength + temp.Length >= length)
                
{
                    temp 
= temp.Substring(0, length - maxlength);
                    input 
= input + temp + "" + mx.Value;
                    
break;
                }

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

                //一个先进后出队列,如果发现有结束,那么从堆栈中取出来
                
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);
            }

            
if (maxlength == 0)
            
{
                
return content.Substring(0, length) + "";
            }
              //对先进后出队列中已经压入的开始标签填充结束标签
            
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;
        }

//去掉JS代码
public static string StripScriptTags(string content)
        
{
            
string cleanText;
 
            content 
= Regex.Replace(content, "<script((.|\n)*?)</script>""", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            cleanText 
= Regex.Replace(content, "\"javascript:""", RegexOptions.IgnoreCase | RegexOptions.Multiline);

            
return cleanText;
        }
posted @ 2007-10-25 09:30  克隆  阅读(311)  评论(0编辑  收藏  举报