• ASP.NET特殊字符串替换函数


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;

                /// <summary>
                /// 对字符串进行检查和替换其中的特殊字符
                /// </summary>
                /// <param name="strHtml"></param>
                /// <returns></returns>
                public static string HtmlToTxt(string strHtml)
                {
                    string[] aryReg ={
                            @"<script[^>]*?>.*?</script>",
                            @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
                            @"([\r\n])[\s]+",
                            @"&(quot|#34);",
                            @"&(amp|#38);",
                            @"&(lt|#60);",
                            @"&(gt|#62);",
                            @"&(nbsp|#160);",
                            @"&(iexcl|#161);",
                            @"&(cent|#162);",
                            @"&(pound|#163);",
                            @"&(copy|#169);",
                            @"&#(\d+);",
                            @"-->",
                            @"<!--.*\n"
                            };

                    string newReg = aryReg[0];
                    string strOutput = strHtml;
                    for (int i = 0; i < aryReg.Length; i++)
                    {
                        Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
                        strOutput = regex.Replace(strOutput, string.Empty);
                    }

                    strOutput.Replace("<", "");
                    strOutput.Replace(">", "");
                    strOutput.Replace("\r\n", "");


                    return strOutput;
                }


    =================另外两个函数=====================
        /// <summary>
        /// 替换html中的特殊字符
        /// </summary>
        /// <param name="theString">需要进行替换的文本。</param>
        /// <returns>替换完的文本。</returns>
        public string HtmlEncode(string theString)
        {
            theString = theString.Replace(">", "&gt;");
            theString = theString.Replace("<", "&lt;");
            theString = theString.Replace(" ", "&nbsp;");
            theString = theString.Replace(" ", "&nbsp;");
            theString = theString.Replace("\"", "&quot;");
            theString = theString.Replace("\'", "'");
            theString = theString.Replace("\n", "<br/> ");
            return theString;
        }

        /// <summary>
        /// 恢复html中的特殊字符
        /// </summary>
        /// <param name="theString">需要恢复的文本。</param>
        /// <returns>恢复好的文本。</returns>
        public string HtmlDiscode(string theString)
        {
            theString = theString.Replace("&gt;", ">");
            theString = theString.Replace("&lt;", "<");
            theString = theString.Replace("&nbsp;", " ");
            theString = theString.Replace("&nbsp;", " ");
            theString = theString.Replace("&quot;", "\"");
            theString = theString.Replace("'", "\'");
            theString = theString.Replace("<br/> ", "\n");
            return theString;
        }

  • 相关阅读:
    《代码之道》试读:规范书变更请求
    解读ASP.NET MVC 4 规划路线图
    淘宝数据魔方技术架构解析
    《程序员实用算法》试读:1.2.2主要的优化:函数调用
    《软件框架设计的艺术》试读:2.2 模块化应用程序
    磁盘分割原理
    无锡云计算中心3年内到底做了什么
    模式识别的一些资料
    边缘检测算法
    用递归方法来搜索连通区域
  • 原文地址:https://www.cnblogs.com/xianzuoqiaoqi/p/1450589.html
Copyright © 2020-2023  润新知