asp.net C#过滤html标签几个函数

在项目中遇到这样一个需求,需要将一段html转换为一般文本返回,万能的正则表达式来了。

正则表达式来拯救你,代码如下:

 代码如下 复制代码

public static string Html2Text(string htmlStr)
{

if ( String .IsNullOrEmpty(htmlStr))

{

return "" ;

}

string regEx_style =  "<style[^>]*?>[\\s\\S]*?<\\/style>" ;  //定义style的正则表达式

string regEx_script =  "<script[^>]*?>[\\s\\S]*?<\\/script>" ;  //定义script的正则表达式  

string regEx_html =  "<[^>]+>" ;  //定义HTML标签的正则表达式  

htmlStr =  Regex .Replace(htmlStr, regEx_style,  "" ); //删除css

htmlStr =  Regex .Replace(htmlStr, regEx_script,  "" ); //删除js

htmlStr =  Regex .Replace(htmlStr, regEx_html,  "" ); //删除html标记

htmlStr =  Regex .Replace(htmlStr,  "\\s*|\t|\r|\n" ,  "" ); //去除tab、空格、空行

htmlStr = htmlStr.Replace( " " ,  "" );

htmlStr = htmlStr.Replace( """ ,  "" ); //去除异常的引号" " "

htmlStr = htmlStr.Replace( """ ,  "" );

return htmlStr.Trim();

}

 

代码二

以下是引用片段:
  -----

 代码如下 复制代码

/**/
///   <summary>
///   去除HTML标记
///   </summary>
///   <param   name="NoHTML">包括HTML的源码   </param>
///   <returns>已经去除后的文字</returns>
public static string NoHTML(string Htmlstring)
{
  //删除脚本
  Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "",
    RegexOptions.IgnoreCase);
  //删除HTML
  Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "",
    RegexOptions.IgnoreCase);
  Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "",
    RegexOptions.IgnoreCase);
  Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
  Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"",
    RegexOptions.IgnoreCase);
  Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&",
    RegexOptions.IgnoreCase);
  Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<",
    RegexOptions.IgnoreCase);
  Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">",
    RegexOptions.IgnoreCase);
  Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "   ",
    RegexOptions.IgnoreCase);
  Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1",
    RegexOptions.IgnoreCase);
  Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2",
    RegexOptions.IgnoreCase);
  Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3",
    RegexOptions.IgnoreCase);
  Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9",
    RegexOptions.IgnoreCase);
  Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "",
    RegexOptions.IgnoreCase);

  Htmlstring.Replace("<", "");
  Htmlstring.Replace(">", "");
  Htmlstring.Replace("\r\n", "");
  Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();

  return Htmlstring;
}

/**/ ///提取HTML代码中文字的C#函数
///   <summary>
///   去除HTML标记
///   </summary>
///   <param   name="strHtml">包括HTML的源码   </param>
///   <returns>已经去除后的文字</returns>
using System;
using System.Text.RegularExpressions;
public class StripHTMLTest
{
  public static void Main()
  {
    string s = StripHTML(
      "<HTML><HEAD><TITLE>中国石龙信息平台</TITLE></HEAD><BODY>faddfs龙信息平台</BODY></HTML>");
    Console.WriteLine(s);
  }

  public static string StripHTML(string strHtml)
  {
    string[]aryReg =
    {
      @"<script[^>]*?>.*?</script>",

      @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\["
        "'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>", @"([\r\n])[\s]+", @
        "&(quot|#34);", @"&(amp|#38);", @"&(lt|#60);", @"&(gt|#62);", @
        "&(nbsp|#160);", @"&(iexcl|#161);", @"&(cent|#162);", @"&(pound|#163);",
        @"&(copy|#169);", @"&#(\d+);", @"-->", @"<!--.*\n"
    };

    string[]aryRep =
    {
      "", "", "", "\"", "&", "<", ">", "   ", "\xa1",  //chr(161),
      "\xa2",  //chr(162),
      "\xa3",  //chr(163),
      "\xa9",  //chr(169),
      "", "\r\n", ""
    };

    string newReg = aryReg[0];
    string strOutput = strHtml;
    for (int i = 0; i < aryReg.Length; i++)
    {
      Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
      strOutput = regex.Replace(strOutput, aryRep[i]);
    }
    strOutput.Replace("<", "");
    strOutput.Replace(">", "");
    strOutput.Replace("\r\n", "");
    return strOutput;
  }
}

写一个静态方法移除HTML标签
#region
///移除HTML标签
 /**/ ///   <summary>
///   移除HTML标签
///   </summary>
///   <param   name="HTMLStr">HTMLStr</param>
public static string ParseTags(string HTMLStr)
{
  return System.Text.RegularExpressions.Regex.Replace(HTMLStr, "<[^>]*>", "");
}

#endregion
  
///   取出文本中的图片地址
#region
///   取出文本中的图片地址
 /**/ ///   <summary>
///   取出文本中的图片地址
///   </summary>
///   <param   name="HTMLStr">HTMLStr</param>
public static string GetImgUrl(string HTMLStr)
{
  string str = string.Empty;
  string sPattern = @"^<img\s+[^>]*>";
  Regex r = new Regex(@"<img\s+[^>]*\s*src\s*=\s*([']?)(?<url>\S+)'?[^>]*>",
    RegexOptions.Compiled);
  Match m = r.Match(HTMLStr.ToLower());
  if (m.Success)
    str = m.Result("${url}");
  return str;
}

#endregion

例子三

 代码如下 复制代码

public static string StripHTML(string source)
{

try
{
string result;
result = source.Replace("\r", " ");
result = result.Replace("\n", " ");
result = result.Replace("'", " ");
result = result.Replace("\t", string.Empty);
result = System.Text.RegularExpressions.Regex.Replace(result, @"( )+", " ");
result = System.Text.RegularExpressions.Regex.Replace(result, @"<( )*head([^>])*>","<head>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @"(<( )*(/)( )*head( )*>)","</head>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, "(<head>).*(</head>)",string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @"<( )*script([^>])*>","<script>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @"(<( )*(/)( )*script( )*>)","</script>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @"(<script>).*(</script>)",string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @"<( )*style([^>])*>","<style>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @"(<( )*(/)( )*style( )*>)","</style>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, "(<style>).*(</style>)",string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @"<( )*td([^>])*>","\t", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @"<( )*br( )*>","\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @"<( )*li( )*>","\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @"<( )*div([^>])*>","\r\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @"<( )*tr([^>])*>","\r\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @"<( )*p([^>])*>","\r\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @"<[^>]*>",string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @"&nbsp;"," ", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @"&bull;"," * ", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @"&lsaquo;","<", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @"&rsaquo;",">", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @"&trade;","(tm)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @"&frasl;","/", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @"<","<", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @">",">", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @"&copy;","(c)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @"&reg;","(r)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, @"&(.{2,6});",string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = result.Replace("\n", "\r");
result = System.Text.RegularExpressions.Regex.Replace(result, "(\r)( )+(\r)","\r\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, "(\t)( )+(\t)","\t\t", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, "(\t)( )+(\r)","\t\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, "(\r)( )+(\t)","\r\t", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, "(\r)(\t)+(\r)","\r\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, "(\r)(\t)+","\r\t", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
string breaks = "\r\r\r";
string tabs = "\t\t\t\t\t";
for (int index=0; index<result.Length; index++)
{
result = result.Replace(breaks, "\r\r");
result = result.Replace(tabs, "\t\t\t\t");
breaks = breaks + "\r";
tabs = tabs + "\t";
}
return result;
}
catch
{
//MessageBox.Show("Error");
return source;
}

}

时间: 2024-08-23 16:33:15

asp.net C#过滤html标签几个函数的相关文章

js字符过滤html标签互转函数

js字符过滤html标签互转函数 function htmlencode(str) {  str = str.replace(/&/g, '&');  str = str.replace(/</g, '<');  str = str.replace(/>/g, '>');  str = str.replace(/(?:t| |v|r)*n/g, '<br />');  str = str.replace(/  /g, '  ');  str = str.

asp 过滤html标签

这是四款asp 过滤html标签函数与方法,这些大全部都是利用正则表达式来过滤以<与>的数据,然后替换里面的东西,这样asp过滤html的函数不成功了. function htmlencode(text) { return text.replace(/&/g, '&amp').replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>'); } function htmldecode(

ASP.NET过滤HTML标签只保留换行与空格的方法_实用技巧

本文实例讲述了ASP.NET过滤HTML标签只保留换行与空格的方法.分享给大家供大家参考.具体分析如下: 自己从网上找了一个过滤HTML标签的方法,我也不知道谁的才是原创的,反正很多都一样.我把那方法复制下来,代码如下: 复制代码 代码如下: ///   <summary> ///   去除HTML标记 ///   </summary> ///   <param name="NoHTML">包括HTML的源码   </param> ///

asp.net自定义函数过滤HTML标签只保留换行与空格

自己从网上找了一个过滤HTML标签的方法,我也不知道谁的才是原创的,反正很多都一样.我把那方法复制下来,代码如下:  代码如下 复制代码 ///   <summary> ///   去除HTML标记 ///   </summary> ///   <param   name="NoHTML">包括HTML的源码   </param> ///   <returns>已经去除后的文字</returns> public s

javascript过滤html标签代码

<head> <meta http-equiv="content-type" content="text/html; charset=gb2312" /> <title>javascript教程过滤html标签代码</title> <script   language= "javascript "> <!-- function setcontent() { var obj = do

ASP正则表达式清除HTML指定标签的方法_应用技巧

在HTML编辑器发布过程中,会出现一些自动生成的HTML标签,或者在留言板程序应用过程中,也会出现有人恶意写入一些HTML代码的情况,所以怎么精准的过滤掉某些特定的属性标签和参数呢?下面是代码: <% Function ReplaceText(fString,patrn,replStr) Set regEx = New RegExp ' 建立正则表达式. regEx.Pattern = patrn ' 设置模式. regEx.IgnoreCase = True ' 设置是否区分大小写. regE

ASP正则表达式清除HTML指定标签的方法

在HTML编辑器发布过程中,会出现一些自动生成的HTML标签,或者在留言板程序应用过程中,也会出现有人恶意写入一些HTML代码的情况,所以怎么精准的过滤掉某些特定的属性标签和参数呢?下面是代码: <% Function ReplaceText(fString,patrn,replStr) Set regEx = New RegExp ' 建立正则表达式. regEx.Pattern = patrn ' 设置模式. regEx.IgnoreCase = True ' 设置是否区分大小写. regE

php正则过滤html标签、空格、换行符的代码(附说明)

复制代码 代码如下: $str=preg_replace("/\s+/", " ", $str); //过滤多余回车 $str=preg_replace("/<[ ]+/si","<",$str); //过滤<__("<"号后面带空格) $str=preg_replace("/<\!--.*?-->/si","",$str); //

php过滤HTML标签、属性等正则表达式汇总_php实例

$str=preg_replace("/\s+/", " ", $str); //过滤多余回车 $str=preg_replace("/<[ ]+/si","<",$str); //过滤<__("<"号后面带空格)   $str=preg_replace("/<\!--.*?-->/si","",$str); //注释 $str=p