ユーザー向けのコメント領域を含む Web サイトを作成しています。ゲストブックや製品レビューの例。また、ユーザーがコメント欄に不適切な言葉を投稿することを制限したいと考えています。例:下品。
ユーザーが下品な言葉を入力すると、文字は * に置き換えられます。*例 - バカから s * * * * **.
関連サイトで調べていたのですが、だめでした。これに関する提案やチュートリアルは大歓迎です。
「悪い言葉」の使用を完全に止める方法はありませんが、すべての行に悪い言葉を含むテキスト ファイルを作成することで、使用を防ぐことができます。次に、単語のリストをファイルからList<String>
プログラムの a にロードします。これを行うには、次のようにします。
// 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;
}