asp.net如何过滤掉html代码

html-css09

asp.net如何过滤掉html代码,第1张

public static string NoHtml(string text)

{

//删除脚本

text = Regex.Replace(text, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase)

//删除HTML

text = Regex.Replace(text, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"-->", "", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"<!--.*", "", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"&(quot|#34)", "\"", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"&(amp|#38)", "&", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"&(lt|#60)", "<", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"&(gt|#62)", ">", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"&(nbsp|#160)", " ", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"&(iexcl|#161)", "\xa1", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"&(cent|#162)", "\xa2", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"&(pound|#163)", "\xa3", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"&(copy|#169)", "\xa9", RegexOptions.IgnoreCase)

text = Regex.Replace(text, @"(\d+)", "", RegexOptions.IgnoreCase)

text.Replace("<", "")

text.Replace(">", "")

text.Replace("\r\n", "")

text = HttpContext.Current.Server.HtmlEncode(text).Trim()

return text

}

没什么意义的,别人到你网站上打开CSS,然后另存一下,上传到自己服务器,再引用不就行了?

css是个直接显示在客户端的东西,别人没有必要盗用,而且用asp判断,效率低下,有专门的防盗链软件,下载一个就行了。

方法一<%

Function ClearHtml(Content)

Content=Zxj_ReplaceHtml("[^>]*", "", Content)

Content=Zxj_ReplaceHtml("</?marquee[^>]*>", "", Content)

Content=Zxj_ReplaceHtml("</?object[^>]*>", "", Content)

Content=Zxj_ReplaceHtml("</?param[^>]*>", "", Content)

Content=Zxj_ReplaceHtml("</?embed[^>]*>", "", Content)

Content=Zxj_ReplaceHtml("</?table[^>]*>", "", Content)

Content=Zxj_ReplaceHtml(" ","",Content)

Content=Zxj_ReplaceHtml("</?tr[^>]*>", "", Content)

Content=Zxj_ReplaceHtml("</?th[^>]*>","",Content)

Content=Zxj_ReplaceHtml("</?p[^>]*>","",Content)

Content=Zxj_ReplaceHtml("</?a[^>]*>","",Content)

Content=Zxj_ReplaceHtml("</?img[^>]*>","",Content)

Content=Zxj_ReplaceHtml("</?tbody[^>]*>","",Content)

Content=Zxj_ReplaceHtml("</?li[^>]*>","",Content)

Content=Zxj_ReplaceHtml("</?span[^>]*>","",Content)

Content=Zxj_ReplaceHtml("</?div[^>]*>","",Content)

Content=Zxj_ReplaceHtml("</?th[^>]*>", "", Content)

Content=Zxj_ReplaceHtml("</?td[^>]*>", "", Content)

Content=Zxj_ReplaceHtml("</?script[^>]*>", "", Content)

Content=Zxj_ReplaceHtml("(javascript|jscript|vbscript|vbs):", "", Content)

Content=Zxj_ReplaceHtml("on(mouse|exit|error|click|key)", "", Content)

Content=Zxj_ReplaceHtml("<\\?xml[^>]*>", "", Content)

Content=Zxj_ReplaceHtml("<\/?[a-z]+:[^>]*>", "", Content)

Content=Zxj_ReplaceHtml("</?font[^>]*>", "", Content)

'Content=Zxj_ReplaceHtml("</?b[^>]*>","",Content)

Content=Zxj_ReplaceHtml("</?u[^>]*>","",Content)

Content=Zxj_ReplaceHtml("</?i[^>]*>","",Content)

'Content=Zxj_ReplaceHtml("</?strong[^>]*>","",Content)

ClearHtml=Content

End Function

Function Zxj_ReplaceHtml(patrn, strng,content)

IF IsNull(content) Then

content=""

End IF

Set regEx = New RegExp ' 建立正则表达式。

regEx.Pattern = patrn ' 设置模式。

regEx.IgnoreCase = true ' 设置忽略字符大小写。

regEx.Global = True ' 设置全局可用性。

Zxj_ReplaceHtml=regEx.Replace(content,strng) ' 执行正则匹配

End Function

%><%nr=ClearHtml(rs("content"))%><%left(nr,100)%>方法二<%Function RemoveHTML(strHTML)

Dim objRegExp, Match, Matches

Set objRegExp = New Regexp objRegExp.IgnoreCase = True

objRegExp.Global = True objRegExp.Pattern = "<.+?>" Set Matches = objRegExp.Execute(strHTML) For Each Match in Matches

strHtml=Replace(strHTML,Match.value,"")

Next

RemoveHTML=strHTML

Set objRegExp = Nothing

End Function

%><%nr=RemoveHTML(rs("content"))%><%left(nr,100)%>先过滤格式 再截取字符 就不会产错位或撑开页面了。