Common下StringHelper.cs 对html标签过滤
来源:互联网 发布:linux shell 执行状态 编辑:程序博客网 时间:2024/05/18 19:37
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text.RegularExpressions;using System.Web;namespace Common{ public class StringHelper { public static string SubStr(string sString, int nLeng) { if (sString.Length <= nLeng) { return sString; } else { return sString.Substring(0, nLeng) + "..."; } } /// <summary> /// 清除脚本 /// </summary> /// <param name="Htmlstring"></param> /// <returns></returns> 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|#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> /// 生成指定长度的随机数字串 ///</summary> ///<param name="length">长度</param> ///<returns></returns> public static string RndStr(int length) { Random rnd = new Random(); string returnstring = ""; int i = 1; while (i <= length) { returnstring += rnd.Next(0, 9).ToString(); i++; } return returnstring; } /// <summary> /// 截取字符串,保留其HTML格式 /// </summary> /// <param name="strText"></param> /// <param name="len"></param> /// <returns></returns> public static string CutWithSubstring(string strText, int len) { if (strText.Length > len) { return strText.Substring(0, len) + "…"; } else { return strText; } } public static string LoseHtml(string ContentStr) { string ClsTempLoseStr = ""; System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex("<///*[^<>]*>"); ClsTempLoseStr = regEx.Replace(ContentStr, ""); return ClsTempLoseStr; } /// <summary> /// 截取字符串去掉其HTML字符 /// </summary> /// <param name="inputString"></param> /// <param name="len"></param> /// <returns></returns> public static string CutWithOutHtml(string inputString, int len) { int tempLen = 0; string tempString = ""; inputString = LoseHtml(inputString); ASCIIEncoding ascii = new ASCIIEncoding(); byte[] s = ascii.GetBytes(inputString); for (int i = 0; i < s.Length; i++) { if ((int)s[i] == 63) { tempLen += 2; } else { tempLen += 1; } try { tempString += inputString.Substring(i, 1); } catch { break; } if (tempLen > len) break; } //如果截过则加上半个省略号 byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString); if (mybyte.Length > len) { tempString = tempString.Substring(0, tempString.Length - 3 == 0 ? 1 : tempString.Length - 3) + "…"; } return tempString; } }}
0 0
- Common下StringHelper.cs 对html标签过滤
- Common下StringHelper.cs
- Common下StringHelper.cs
- StringHelper.cs
- C# 中对html 标签过滤
- Common下FileHelper.cs
- Common下ImageHelper.cs
- Common下JsonHelper.cs
- Common下MadieHelper.cs
- Common下MessageBox.cs
- Common下RegHelper.cs
- Common下JsonHelper.cs
- Common下MessageBox.cs
- Common下JsonHelper.cs
- Common下MadieHelper.cs
- Common下RegHelper.cs
- ASP 过滤 HTML 标签
- .net 过滤html标签
- 最简单的基于FFmpeg的封装格式处理:视音频复用器(muxer)
- USB协议架构及驱动架构
- 口碑营销应从网站自身做起,完善用户体验优化产品服务
- win32程序中如何使用GDIPlus
- 机器学习入门:Linear Regression与Normal Equation
- Common下StringHelper.cs 对html标签过滤
- xxx
- mysql创建定时任务
- uva 1572
- ThinkPHP分页使用例子(二十一)
- Could not create the view: An unexpected exception was thrown
- 网站做软文推广应该软到什么程度
- 多线程(互斥锁、读写锁、条件变量、信号量、自旋锁)相关
- _makepath and _splitpath 分析路径