c#

位置:IT落伍者 >> c# >> 浏览文章

C#过滤HTML代码


发布日期:2020年06月24日
 
C#过滤HTML代码

public string NoHTML(string Htmlstring)

//去除HTML标记

{

//删除脚本

Htmlstring = RegexReplace(Htmlstring@<script[^>]*?>*?</script>RegexOptionsIgnoreCase);

//删除HTML

Htmlstring = RegexReplace(Htmlstring@<([^>]*)>RegexOptionsIgnoreCase);

Htmlstring = RegexReplace(Htmlstring@([\r\n])[\s]+RegexOptionsIgnoreCase);

Htmlstring = RegexReplace(Htmlstring@>RegexOptionsIgnoreCase);

Htmlstring = RegexReplace(Htmlstring@<!*RegexOptionsIgnoreCase);

Htmlstring = RegexReplace(Htmlstring@&(quot|#);\RegexOptionsIgnoreCase);

Htmlstring = RegexReplace(Htmlstring@&(amp|#);&RegexOptionsIgnoreCase);

Htmlstring = RegexReplace(Htmlstring@&(lt|#);<RegexOptionsIgnoreCase);

Htmlstring = RegexReplace(Htmlstring@&(gt|#);>RegexOptionsIgnoreCase);

Htmlstring = RegexReplace(Htmlstring@&(nbsp|#); RegexOptionsIgnoreCase);

Htmlstring = RegexReplace(Htmlstring@&(iexcl|#);\xaRegexOptionsIgnoreCase);

Htmlstring = RegexReplace(Htmlstring@&(cent|#);\xaRegexOptionsIgnoreCase);

Htmlstring = RegexReplace(Htmlstring@&(pound|#);\xaRegexOptionsIgnoreCase);

Htmlstring = RegexReplace(Htmlstring@&(copy|#);\xaRegexOptionsIgnoreCase);

Htmlstring = RegexReplace(Htmlstring@&#(\d+);RegexOptionsIgnoreCase);

HtmlstringReplace(<);

HtmlstringReplace(>);

HtmlstringReplace(\r\n);

Htmlstring=HttpContextCurrentServerHtmlEncode(Htmlstring)Trim();

return Htmlstring;

}

               

上一篇:C#中如何编写使用SendMessage

下一篇:.NET框架类小结