想要屏蔽违规关键字,单独建一个类 RemoveKeyword文件来过滤这些关键字,例如 首页 列表页 内容页 中 都有一个想要过滤的关键字“AAAA“
第一步:建立一个txt文件存储敏感关键字。用逗号分开。
AAA,BBB
第二步:读取txt文件的内容,然后以逗号为分割线,把敏感词组成一个数组。第三步:用一个foreach 遍历数组,然后循环判断是否有敏感词。
如上代码应该怎么写怎么读取。数字怎么设置。
第一步:建立一个txt文件存储敏感关键字。用逗号分开。
AAA,BBB
第二步:读取txt文件的内容,然后以逗号为分割线,把敏感词组成一个数组。第三步:用一个foreach 遍历数组,然后循环判断是否有敏感词。
如上代码应该怎么写怎么读取。数字怎么设置。
这两行代码应该怎么写呢。
string regex="AAA,BBBB";
string[] regs = regex.Split(',');
foreach (string reg in regs)
{
if (str.Contains(regex))
{
str.Replace(regex, "@#$%");
}
}
foreach (string reg in regs)
{
if (str.Contains(reg))
{
str.Replace(reg, "@#$%");
}
}
这个真“无奈”啊。告诉你一点点,假设敏感词在数组a[],判断并且过滤字符串变量s中的敏感词,最直观的写法似乎是:public string 过滤敏感词(string[] a, string s)
{
for(var i=0; i<a.length; i++)
s=s.Replace(a[i],new string('*',a[i].length)); //把敏感词替换为多个*。
}
这样,变量a中就保存有一个string[]数组,其中是敏感词。
{
string[] BadWords = new string[] { "a", "h", "n", "m", "i", "g", "j", "k", "q", "y", "o", "p", "c" }; for (int i = 0; i < BadWords.Length; i++) { s = s.Replace(BadWords[i], new String('*', BadWords[i].Length)); } return s; }
void Application_BeginRequest(object sender, EventArgs e)
{
for (int i=0; i < Request.Form.Count;i++)
{
if (Request.Form[i].ToString() == "__VIEWSTATE") continue;
if (IsM(Request.Form[i].ToString()))
{
Response.Write("您提交的内容中含有非法字符.");
Response.End();
} }
}
protected bool IsM(string InText)
{
string word = @"";
if (InText == null)
return false;
if (Regex.IsMatch(InText,word))
return true;
return false;
}