入门 硬件知识 电脑基础 上网入门 故障 电脑修护 电脑健康 精通电脑 网站 DIV+CSS 建站知识 SEO知识
教程 电脑操作 平面设计 路由设置 技巧 Word Excel QQ技巧 壁纸 自然风景 酷车美女 系统桌面
系统 安装系统 系统技巧 系统设置 安全 木马查杀 黑客防御 安全资讯 美女 明星写真 清纯美女 性感美女

主页 > 电脑编程 > INTRODUCE

C#特殊字符过滤函数

发布时间:2013-04-07 作者:电脑知识网 来源:www.sogoupc.com 字号:

C#特殊字符过滤函数

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;
    }


文章: C#特殊字符过滤函数
地址:http://www.sogoupc.com/pcbc/201304/1839.html
------分隔线----------------------------
------分隔线----------------------------
栏目分类
热门电脑编程文章推荐