public static string GetSafeHtml(string val) { if (string.IsNullOrEmpty(val)) { return string.Empty; } var v1 = Sanitizer.GetSafeHtmlFragment(val);//安全字符 var v2 = SanitizerCompatibleWithChineseCharacters(v1);//中文乱码 //Regex objRegEx=new Regex("<([^>]+)( [^>]+)>");<(?!img|a|\a)([^>]+)( [^>]+)> Regex objRegEx = new Regex("<(?!img|a|\a|font)([^>]+)( [^>]+)>");//过滤HTML标签属性 Regex objRegEx2 = new Regex("<[^>^/]+> ]+>");//过滤空白HTML标签 var v3 = objRegEx.Replace(v2, "<$1>"); var v4 = objRegEx2.Replace(v3, ""); return v4; }
PS: SanitizerCompatibleWithChineseCharacters 参见本博客.