//清除HTML函數
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|#
);
\
RegexOptions
IgnoreCase);
Htmlstring = Regex
Replace(Htmlstring
@
&(amp|#
);
&
RegexOptions
IgnoreCase);
Htmlstring = Regex
Replace(Htmlstring
@
&(lt|#
);
<
RegexOptions
IgnoreCase);
Htmlstring = Regex
Replace(Htmlstring
@
&(gt|#
);
>
RegexOptions
IgnoreCase);
Htmlstring = Regex
Replace(Htmlstring
@
&(nbsp|#
);
RegexOptions
IgnoreCase);
Htmlstring = Regex
Replace(Htmlstring
@
&(iexcl|#
);
\xa
RegexOptions
IgnoreCase);
Htmlstring = Regex
Replace(Htmlstring
@
&(cent|#
);
\xa
RegexOptions
IgnoreCase);
Htmlstring = Regex
Replace(Htmlstring
@
&(pound|#
);
\xa
RegexOptions
IgnoreCase);
Htmlstring = Regex
Replace(Htmlstring
@
&(copy|#
);
\xa
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;
}
From:http://tw.wingwit.com/Article/program/net/201311/15282.html