ASP.NET过滤HTML字符串方法总结
程序员文章站
2024-02-22 08:15:34
本文实例讲述了asp.net过滤html字符串的方法,供大家参考使用,具体代码如下:
/// 去除html标记
///...
本文实例讲述了asp.net过滤html字符串的方法,供大家参考使用,具体代码如下:
/// <summary>去除html标记 /// /// </summary> /// <param name="htmlstring">包括html的源码</param> /// <returns>已经去除后的文字</returns> public static string getnohtmlstring(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; } /// <summary>获取显示的字符串,可显示html标签,但把危险的html标签过滤,如iframe,script等。 /// /// </summary> /// <param name="str">未处理的字符串</param> /// <returns></returns> public static string getsafehtmlstring(string str) { str = regex.replace(str, @"<applet[^>]*?>.*?</applet>", "", regexoptions.ignorecase); str = regex.replace(str, @"<body[^>]*?>.*?</body>", "", regexoptions.ignorecase); str = regex.replace(str, @"<embed[^>]*?>.*?</embed>", "", regexoptions.ignorecase); str = regex.replace(str, @"<frame[^>]*?>.*?</frame>", "", regexoptions.ignorecase); str = regex.replace(str, @"<script[^>]*?>.*?</script>", "", regexoptions.ignorecase); str = regex.replace(str, @"<frameset[^>]*?>.*?</frameset>", "", regexoptions.ignorecase); str = regex.replace(str, @"<html[^>]*?>.*?</html>", "", regexoptions.ignorecase); str = regex.replace(str, @"<iframe[^>]*?>.*?</iframe>", "", regexoptions.ignorecase); str = regex.replace(str, @"<style[^>]*?>.*?</style>", "", regexoptions.ignorecase); str = regex.replace(str, @"<layer[^>]*?>.*?</layer>", "", regexoptions.ignorecase); str = regex.replace(str, @"<link[^>]*?>.*?</link>", "", regexoptions.ignorecase); str = regex.replace(str, @"<ilayer[^>]*?>.*?</ilayer>", "", regexoptions.ignorecase); str = regex.replace(str, @"<meta[^>]*?>.*?</meta>", "", regexoptions.ignorecase); str = regex.replace(str, @"<object[^>]*?>.*?</object>", "", regexoptions.ignorecase); return str; }
上一篇: ubuntu下没有中文输入法的解决办法
推荐阅读
-
Asp.net中将Word文件转换成HTML的方法
-
asp.net利用后台实现直接生成html分页的方法
-
C#检测是否有危险字符的SQL字符串过滤方法
-
ASP.NET批量操作基于原生html标签的无序列表的三种方法
-
asp.net mvc下拉框Html.DropDownList 和DropDownListFor的常用方法
-
对Java字符串与整形、浮点类型之间的相互转换方法总结
-
php实现过滤表单提交中html标签的方法,表单标签_PHP教程
-
php实现过滤表单提交中html标签的方法_PHP
-
ASP.NET过滤HTML标签只保留换行与空格的方法
-
PHP入门教程之字符串处理技巧总结(转换,过滤,解析,查找,截取,替换等)