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

推荐订阅源

有赞技术团队
有赞技术团队
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Palo Alto Networks Blog
C
Cisco Blogs
The Hacker News
The Hacker News
T
Threatpost
S
Schneier on Security
K
Kaspersky official blog
Spread Privacy
Spread Privacy
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
NISL@THU
NISL@THU
量子位
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google DeepMind News
Google DeepMind News
Security Latest
Security Latest
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
博客园 - 叶小钗
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
News and Events Feed by Topic
爱范儿
爱范儿
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
Project Zero
Project Zero
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cisco Talos Blog
Cisco Talos Blog
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
T
Tenable Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Vulnerabilities – Threatpost
Forbes - Security
Forbes - Security
博客园 - 三生石上(FineUI控件)
C
Cyber Attacks, Cyber Crime and Cyber Security
N
News and Events Feed by Topic
V
V2EX
Webroot Blog
Webroot Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Blog — PlanetScale
Blog — PlanetScale
M
MIT News - Artificial intelligence
Scott Helme
Scott Helme
Simon Willison's Weblog
Simon Willison's Weblog
L
LangChain Blog
W
WeLiveSecurity
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 最后更新