日期:2014-05-18  浏览次数:21251 次

怎样批量屏蔽网页关键字
需要屏蔽很多关键字
关键字以分号分看 

“湖北,石首,武警;换妻;黄白,牙签;黄樟;婚外恋;”
  这里有5个关键字 分别以分号";"隔开
  ","逗号前后的字词不能同时在一个网页上,不论相隔多远

有什么好的办法可以批量处理,应为需要处理的关键字太多了,有好几百个。

------解决方案--------------------
html:

<%@ Page Language="C#" EnableViewState="false" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebTest.WebForm1" %>

<!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>
“湖北,石首,武警;换妻;黄白,牙签;黄樟;婚外恋;”


“湖北,石首,武警;换妻;黄白,牙签;黄樟;婚外恋;”


“,石首,武警;换妻;黄白,牙签;黄樟;婚外恋;”
“湖北,石首,武警;换妻;黄白,牙签;黄樟;婚外恋;”
</body>
</html>


cs:


protected override void Render(HtmlTextWriter writer)
{
string content = string.Empty;
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
try
{
// 将当前页面的内容呈现到临时的 HtmlTextWriter 对象中
base.Render(htmlWriter);
htmlWriter.Close();
// 得到当前页面的全部内容
content = stringWriter.ToString();
// 替换页面中的部分内容



string[] strs = "湖北,石首,武警;换妻;黄白,牙签;黄樟;婚外恋;".Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

Dictionary<string, string> dic = new Dictionary<string, string>();
string newContent = content;
string[] every;
bool flag = false;
for (int i = 0; i < strs.Length; i++)
{
every = strs[i].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

for (int j = 0; j < every.Length; j++)
{
if (!flag)
{
if (newContent.Contains(every[j]))
{
flag = true;
continue;
}
}
else
{
newContent = newContent.Replace(every[j], "***");
}
}

flag = false;
}



// 将新页面的内容显示出来
writer.Write(newContent);
}
catch { }
finally
{
stringWriter.Dispose();
htmlWriter.Close();
htmlWriter.Dispose();
}
}


参考:
http://blog.csdn.net/net_lover/article/details/5324455