环境:ASP.NET C#
目标:做一个留言本,为了防止某些特殊的关键词汇,采用字典的方式过滤关键词,一旦发现用户提交的文字有符合字典,那么该用户的IP就被记录进数据库
其他:
字典名字为zang.txt 里面的格式为:关键词1|关键词2|关键词3
留言板的内容提交部分为<asp:textbox id="content"></asp:textbox>请问怎么有效的用字典来查询用户提交的内容是否符合?这段代码该怎么样写?谢谢

解决方案 »

  1.   

    the classic method:Read the file,then you get the bad words from this file,
    save as a String.when someone submit this content.then we check from keys what we have got.File file=new File(filepath);
    ......String badKeys=.....//.split(',');for(int i=0;i<badKeys.Length;i++){
     if(content.IndexOf(badKeys[i]!=-1){
       //print the error msg 
     }
    }
      

  2.   

    String badKeys=.....//.split(','); 错了..
    应该为 String [] badKeys=.....//.split(','); 
      

  3.   

    str = str.Replace("'", "''");
      

  4.   

    TO 1,2楼,谢谢了,但是能否写一下更具体?我是菜鸟不是很明白,比如说
    File file是字典路径吧,那么badKeys是不是提交的内容?
    String [] badKeys= 怎么跟字典文件相连,
     
      

  5.   

            //获得关键子
            public int CheckKey(string Content)
            {
                int n = -1;
                DataRowCollection ObjRows = ObjRe.GetReKey().Rows;
                if (ObjRows.Count > 0)
                {
                    string str = ObjRows[0]["key_Content"].ToString();
                    //key_Content自己定义的应该过滤的关键字,返回值为-1说明没有关键字,不为-1说明有关键字
                    String[] Array = str.Split('*');
                    foreach (string item in Array)
                    {
                        int index = Content.IndexOf(item);
                        if (index==0)
                        {
                            n = index;
                            break;
                        }                }
                  
                }
                return n;
               
            }
      

  6.   

    页面部分:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="textHelp.aspx.cs" Inherits="teachDemo_textHelp" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>    
            <asp:TextBox ID="content" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
        </form>
    </body>
    </html>
    代码部分:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.IO;
    public partial class teachDemo_textHelp : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void Button1_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream(Server.MapPath("zang.txt"),FileMode.Open);
            StreamReader sr = new StreamReader(fs);
           string[] words= sr.ReadToEnd().Split('|');
           bool find = false;
           foreach (string s in words)
           {
               if (content.Text == s)
               {
                   find = true;
                   break;
               }
           }
           if (find)
           {
               Response.Write("内容符合:)");
           }
           else
           {
               Response.Write("内容不符合!");
           }
           fs.Close();
           sr.Close();
        }
    }记得给分哦:)
      

  7.   

    关键词可以使用enum
    [Flag]
    enum KeyWord
    {
    key1,key2,key3
    }
    然后使用&操作,判断包含几个关键词