• .net工具类——HTML处理


            #region 截取字符长度
    
            /// <summary>
            /// 截取字符长度
            /// </summary>
            /// <param name="inputString">字符</param>
            /// <param name="len">长度</param>
            /// <returns></returns>
            public static string SubString(string inputString, int len)
            {
                if (string.IsNullOrEmpty(inputString))
                    return "";
                inputString = DropHTML(inputString);
                ASCIIEncoding ascii = new ASCIIEncoding();
                int tempLen = 0;
                string tempString = "";
                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 += "";
                return tempString;
            }
    
            #endregion
    
            #region 清除HTML标记
    
            public static string DropHTML(string Htmlstring)
            {
                if (string.IsNullOrEmpty(Htmlstring)) return "";
                //删除脚本
                Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
                //删除HTML
                Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"([
    ])[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("
    ", "");
                Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
                return Htmlstring;
            }
    
            #endregion
    
            #region 清除HTML标记且返回相应的长度
    
            public static string DropHTML(string Htmlstring, int strLen)
            {
                return SubString(DropHTML(Htmlstring), strLen);
            }
    
            #endregion
    
            #region TXT代码转换成HTML格式
    
            /// <summary>
            /// 字符串字符处理
            /// </summary>
            /// <param name="chr">等待处理的字符串</param>
            /// <returns>处理后的字符串</returns>
            /// //把TXT代码转换成HTML格式
            public static String ToHtml(string Input)
            {
                StringBuilder sb = new StringBuilder(Input);
                sb.Replace(" ", "&nbsp;");
                //sb.Replace("&", "&amp;");
                sb.Replace("<", "&lt;");
                sb.Replace(">", "&gt;");
                sb.Replace("
    ", "<br />");
                sb.Replace("
    ", "<br />");
                sb.Replace("	", " ");
                return sb.ToString();
            }
    
            #endregion
    
            #region HTML代码转换成TXT格式
    
            /// <summary>
            /// 字符串字符处理
            /// </summary>
            /// <param name="chr">等待处理的字符串</param>
            /// <returns>处理后的字符串</returns>
            /// //把HTML代码转换成TXT格式
            public static String ToTxt(String Input)
            {
                StringBuilder sb = new StringBuilder(Input);
                sb.Replace("&nbsp;", " ");
                sb.Replace("<br>", "
    ");
                sb.Replace("<br>", "
    ");
                sb.Replace("<br />", "
    ");
                sb.Replace("<br />", "
    ");
                sb.Replace("&lt;", "<");
                sb.Replace("&gt;", ">");
                sb.Replace("&amp;", "&");
                return sb.ToString();
            }
    
            #endregion
  • 相关阅读:
    【插件】博客园markdown编辑器自定义代码黑色背景高亮显示
    【MatrixSynapseChat】安装教程(一)基于centOS7.* 安装Synapse
    【Python3】python安装 matrix-synapse[all] 报错’Error: pg_config executable not found‘
    【python3】在CentOS7上升级SQLite,并让Python3使用新版SQLite
    【linux】CentOS7 升级sqlite3
    【Python3】Centos7 安装Python3.7
    面试官:兄弟,说说Java的static关键字吧
    六一儿童节,程序员写给女儿的一封信
    被缠上了,小王问我怎么在 Spring Boot 中使用 JDBC 连接 MySQL
    女生适合学编程吗?
  • 原文地址:https://www.cnblogs.com/amusement1992/p/13496235.html
Copyright © 2020-2023  润新知