怎样让自己的网站避免暴力,肮脏的词语袭击?过滤用户提交的数据显得尤为重要。下面是一个非常简单的过滤方法。
public class WordsFilter
{
public string [] filterWords;
private string replaceWords;
/// /// 添加过滤词汇
/// /// 过滤目标(以逗号隔开) /// 替换词 public void InsertFilterWords(string words,string replaceWords)
{
this .replaceWords = replaceWords;
filterWords = words.Split( ',' );
}
/// /// 过滤,返回过滤后的字符串
/// /// /// public string Filter(string msg)
{
for ( int i = 0; i < filterWords.Length; i++)
{
msg=msg.Replace(filterWords[i], replaceWords);
}
return msg;
}
}
|
添加要过滤的词汇
调用Filter方法过滤内容,返回过滤后的内容。
|