过滤非法字符
来源:互联网 发布:外汇交易员 知乎 编辑:程序博客网 时间:2024/05/01 13:50
/// <summary>过滤脏字将字体替换
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string FilterKeyWord(string text, string repStr)
{
string input = text;
for (int i = 0; i < repStr.Split('|').Length; i++)
{
MatchCollection matches = null;
string temp = protectHtml(input, ref matches);
temp = Filter(temp, repStr.Split('|')[i]);
input = restoreHtml(temp, matches);
}
return input;
}
private static string protectHtml(string input, ref MatchCollection matches)
{
//匹配html的正则
Regex htmlReg =
new Regex(@"/<.*?/>", RegexOptions.Multiline);
//获取匹配集合
matches = htmlReg.Matches(input);
//设置替换字串
string markFormat = "[[{0}]]";
//替换html,记录位置
for (int i = 0; i < matches.Count; i++)
{
input = input.Replace(matches[i].Value, string.Format(markFormat, i));
}
return input;
}
private static string restoreHtml(string input, MatchCollection matches)
{
//设置替换字串
string markFormat = "[[{0}]]";
for (int i = 0; i < matches.Count; i++)
{
input = input.Replace(string.Format(markFormat, i), matches[i].Value);
}
return input;
}
private static string Filter(string input, string replace)
{
//设置星号
string replaceformat = "*";
Regex reg = new Regex(String.Format("{0}", replace), RegexOptions.Multiline);
return reg.Replace(input, string.Format(replaceformat, replace));
}
调用
string badStr = "<p>adfsf变化色和sss性<a href='#?id=2008'>年份</a></p>";
Response.Write("<br/>" + FilterKeyWord(badStr , new Config().Badtext));
一般把脏话写在xml,new Config().Badtext这个我就不具体,
楼主测试时候
Response.Write("<br/>" + FilterKeyWord(badStr ,"色|性" ));
- 过滤非法字符 一
- 过滤非法字符 二
- 过滤非法字符 三
- asp过滤非法字符
- Filter过滤非法字符
- 过滤非法字符
- 过滤非法字符
- 过滤非法字符问题
- 过滤非法字符
- java过滤非法字符
- 过滤xml非法字符
- 过滤非法字符
- servlet过滤非法字符
- dom4j 非法字符过滤
- 过滤非法字符函数
- 过滤文件名非法字符
- .net过滤非法字符
- 过滤xml非法字符
- webkit中FastMalloc的分析
- flex 拖放的实现
- PL/SQL Devloper--比较对象/数据
- ssh自动连接 (密钥对),同时ssh控制多台主机,两台交互式控制
- 排序算法汇总(选择排序 ,直接插入排序,冒泡排序,希尔排序,快速排序...)
- 过滤非法字符
- load data infile 优化mysql
- [android反编译小结]apktool/ AXMLPrinter2.jar/ dex2jar.bat/ jd-gui/ Jodeclipse/ JadClipse
- gethostbyname函数
- ADO访问数据库公共类
- 编译QT出现/usr/lib/libstdc++.so.6:version 'GLIBCXX_3.4.9' not found /usr/lib/libstdc++.so.6:version 'GLIBCXX_3.4.11'not found的解决
- C++ 中int,char,string,CString类型转换
- Linux命令之make - 强大的编译工具
- poj1017——Packets