工作室
  我们追求完美的用户体验,提供高品质的网站制作、网站营销、SEO搜索引擎优化,资深的网页设计师+专业的程序员,让您的网站在营销网路上充满动力。 龙翔九度网站工作室服务理念:优质、快捷、诚信。

去除网站中HTML标签的正则表达式

Image is everything
Date:2009-08-10  Type:网站知识

如何去除网站中HTML标签的正则表达式,在制作网站时,我们经常需要把一篇文章的部分片段展示出来,一般是截取文章前面的部分文字。但截取显示出的文字会把文章中的HTML代码显示出来,这样形成了乱码。

去除网站中HTML标签,一般使用正则表达式。去除了网站中的HTML标签,才能把截取的文字正确的显示出来。去除文章中的HTML标签也可以在网站后台中去除,但这样一个个的去除不仅效率低下,而且经常会出错。.net网站中可以使用以下正则表达式来去除文章中的HTML标签,代码如下。


string str = a.ToString();


str = Regex.Replace(str, @"</?span[^>]*>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"&#[^>]*;", "", RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"</?marquee[^>]*>", "", RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"</?object[^>]*>", "", RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"</?param[^>]*>", "", RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"</?embed[^>]*>","",RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"</?table[^>]*>","",RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"&nbsp;","",RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"</?tr[^>]*>", "", RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"</?th[^>]*>","",RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"</?p[^>]*>","",RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"</?a[^>]*>","",RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"</?img[^>]*>","",RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"</?tbody[^>]*>","",RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"</?li[^>]*>","",RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"</?span[^>]*>","",RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"</?div[^>]*>","",RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"</?th[^>]*>", "", RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"</?td[^>]*>", "", RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"</?script[^>]*>", "", RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"(javascript|jscript|vbscript|vbs):", "", RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"on(mouse|exit|error|click|key)", "", RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"<\\?xml[^>]*>", "", RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"<\/?[a-z]+:[^>]*>", "", RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"</?font[^>]*>", "", RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"</?b[^>]*>","",RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"</?u[^>]*>","",RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"</?i[^>]*>","",RegexOptions.IgnoreCase);  
str = Regex.Replace(str, @"</?strong[^>]*>","",RegexOptions.IgnoreCase);  

以上代码能去除网站文章中常见的HTML代码,去除网站中HTML标签的正则表达式,这样才能使网站便捷的显示出我们需要的内容。

版权所有:©2007 - 2009 龙翔九度网站工作室 加入收藏 QQ在线交流
地址:深圳市龙华街道大浪南路老围新村 电话:15820439962 邮箱:lx9d@163.com QQ:276312661
龙翔九度网站工作室服务理念:优质、快捷、诚信