我正在创建一个网站,其中包括一个评论区的用户。例如留言簿或产品评论。我想限制用户在评论区发布不适当的语言。例如:粗俗。
如果用户输入任何粗俗的内容,字符将被替换为*。*示例-从愚蠢到s ** *。
我一直在相关网站上搜索,但没有结果。在这方面的建议或教程将非常感谢。
发布于 2012-07-15 03:17:52
没有办法完全阻止“坏语言”的使用,但你可以尝试通过创建一个每行都包含一个坏单词的文本文件来阻止它。然后将文件中的单词列表加载到程序中的List<String>中。为此,您可以执行以下操作:
// The list of swear words
List<string> swearWords = new List<string>();
private void GetSwearWords()
{
// Get the path to the file that has the swear words list
string path = <File Path>;
// Open the text file
TextReader reader = new StreamReader(path);
// Loop through each line in the file.
string line = "";
while ((line = reader.ReadLine()) != null)
{
// Lower cases word and removes whitespaces
string word = line.Trim().ToLower();
// Adds the word to the list
swearWords.Add(word);
}
}然后,要确定字符串是否包含这些坏词之一,请执行以下操作:
private bool HasSwearWord(string text)
{
// Splits words, removes whitespace and any punctuation
string[] wordArray = Regex.Split(text, @"\W+");
// Check if any word in the string is a swear word
foreach (string word in wordArray)
{
if (swearWords.Contains(word.ToLower()))
{
return true;
}
}
return false;
}https://stackoverflow.com/questions/11485726
复制相似问题