我收藏的特殊字符过滤函数

news/2024/7/10 3:04:46 标签: string, regex, html
htmledit_views">         这个我这里自己一直用的几个函数,下面贴出来。
       希望尽量不是直接要实例就去用,而不关心他本来的意义和实现思路等。加油!

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

            /// <summary>
            /// 对字符串进行检查和替换其中的特殊字符
            /// </summary>
            /// <param name="strHtml"></param>
            /// <returns></returns>
            public static html" title=string>string HtmlToTxt(html" title=string>string strHtml)
            {
                html" title=string>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"
                        };

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

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


                return strOutput;
            }


=================另外两个函数=====================
    /// <summary>
    /// 替换html中的特殊字符
    /// </summary>
    /// <param name="theString">需要进行替换的文本。</param>
    /// <returns>替换完的文本。</returns>
    public html" title=string>string HtmlEncode(html" title=string>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 html" title=string>string HtmlDiscode(html" title=string>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;
    }


http://www.niftyadmin.cn/n/1412821.html

相关文章

《理想国》笔记

随便记记 第一章&#xff1a;什么是正义 问&#xff1a;什么是正义 回答&#xff1a;正义是欠债还债&#xff1f; 逻辑漏洞&#xff1a;还债行为如果伤害到了对方&#xff0c;那也叫正义吗&#xff1f; 反驳&#xff1a;正义是给朋友好的东西&#xff0c;给敌人坏的东西&…

绑定txt文件到DataGrid

这里是DataGrid,换成GridView也一样将文本文件和aspx文件放在统一文件夹下即可。 ReportFile.txtReportFile.aspx<% Page Language"C#" AutoEventWireup"True" %><html><head><script runat"server">void Page_Load(Ob…

re模块: 正则表达式

什么是正则: 正则就是用一些具有特殊含义的符号组合到一起&#xff08;称为正则表达式&#xff09;来描述字符或者字符串的方法.或者说&#xff1a;正则就是用来描述一类事物的规则.&#xff08;在Python中&#xff09;它内嵌在Python中,并通过 re 模块实现.正则表达式模式被编…

新产品如何做用户增长

定义产品的MVP MVP的意思是最小可行产品。 一般产品在研发和推广前&#xff0c;需要做一些验证。 C端产品&#xff0c;特别是创新的商业模式&#xff0c;在前期最需要验证需求是否存在&#xff0c;商业模式是否成立。基于这个验证的需求来实现最简的MVP&#xff0c;然后慢慢迭…

Delphi2009 反射机制的使用

Delphi2009中添加了一个用于实现反射的单元&#xff0c;即ObjAuto&#xff0c;用它可以轻松的实现反射。对于原本的RTTI&#xff0c;只能够得到published区分符下的属性&#xff0c;而反射机制则可以得到拥有反射支持的类的公开方法&#xff0c;成员变量和属性。下面是一个小例…

第18条:尽量使用不可变对象

1、设计类的时候&#xff0c;应充分运用属性来封装数据。 2、应该尽量把对外公布出来的属性设为只读&#xff0c;而且只在确有必要时才将属性对外公布。 3、若属性仅可于对象内部修改&#xff0c;则在“class-continuation分类”中将其由readonly属性扩展为readwrite属性。 这种…

用户体验设计笔记

听了用户体验设计部门的一场分享&#xff0c;简单记录一下交互设计的一些技巧。 交互设计小技巧 1、交互设计眼中的用户画像&#xff0c;主要面向三种用户 ● 聪明非常忙的新手用户。快速、无痛苦 三个层次&#xff1a;目标&#xff08;动机&#xff09;、任务、行动 ● 沉默…

Delphi2009初体验 - 语言篇 - 体验泛型(一)

快速导航&#xff1a; 一、概述 二、体验TList<T> 三、体验TObjectList<T> 四、TList<T>和TObjectList<T>的区别 五、后记 一、概述 等了几百年&#xff0c;Delphi终于原生的支持泛型了。以前使用Delphi&#xff0c;泛型是不被支持的&…