惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

TaoSecurity Blog
TaoSecurity Blog
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
罗磊的独立博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Stack Overflow Blog
Stack Overflow Blog
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
Security Archives - TechRepublic
Security Archives - TechRepublic
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
The Hacker News
The Hacker News
Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
F
Full Disclosure
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Security @ Cisco Blogs
H
Hacker News: Front Page
L
LangChain Blog
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
B
Blog RSS Feed
H
Heimdal Security Blog
Google Online Security Blog
Google Online Security Blog
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 三生石上(FineUI控件)
V2EX - 技术
V2EX - 技术
V
Vulnerabilities – Threatpost
Help Net Security
Help Net Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Tailwind CSS Blog
W
WeLiveSecurity
T
Tenable Blog
D
DataBreaches.Net
Martin Fowler
Martin Fowler
Cyberwarzone
Cyberwarzone
Cisco Talos Blog
Cisco Talos Blog
S
Secure Thoughts
O
OpenAI News
L
LINUX DO - 热门话题
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
Jina AI
Jina AI
J
Java Code Geeks
Know Your Adversary
Know Your Adversary
IT之家
IT之家
Latest news
Latest news
Cloudbric
Cloudbric

博客园 - yet

利用SQL2005的forxml以及反系列化去替代ORM? 用自己的MVC框架 (小技巧)怎样有选择的去屏蔽验证控件。 高校软件杯作品:DreamChat[校园即时通] 梦凌NET期刊管理系统 一却都好像都在肢解着我 <远程教育系统>体系结构设计 协议分析[高校杯准备] 把.NET程序部署到没有安装.NET Framwork的机器上 项目打包时自定义安装属性,如数据库名等 自动安装SQL Server数据库 在制作打包程序的时候自动在用户计算机中还原SQL数据库 截系统热键 IP多播技术[为软件高校杯做准备] 自定义提供程序控件(续) 自定义提供程序控件 实现支持断点续传多线程下载的 Http Web 客户端工具类() 身份证验证算法 C#编写QQ接口软件--QQ协议篇
怎样去获取文章简介
yet · 2007-10-24 · via 博客园 - yet

     相信大家在做博客,文章管理系统之类的时候经常会遇到这样的问题:
 
   把一些文章抽出来放在首页,当然这些文章要显示内容简介。但是问题来了,这些内容简介有时候有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;
        }


                          

 2007-10-25 最后更新