文章关键字
在做后台文章添加的时候,如果添加的文章内 容中(如内容中有“中国“、”广东“)有数据库待匹配的字符串,如”中国“ 、”广东“等关键字。那么就在文章内容这些关键字中自动加上链接<a href=' 来自数据库'>关键字</a>,并让文章内容中相同的关键字(如文章内容中”中国 “出现多次)只加上一个链接,其它的就不用加链接了。并且还要限定一篇文章 中关键字加上链接的总个数(限8个吧),如数据库中待匹配的关键字有10种, 而文章内容中出现的关键字也有10种,显然超过了限定数8个,那么就只在前面8 种关键字上加上超链接,后面的2种关键字则不加超链接。 --------------------编程问答-------------------- 我的思路是正则+判断之后 动态增加关键字的a标签- -具体等高手- - --------------------编程问答-------------------- - -比较笨的方法...在文章显示的时候,或者有做静态化的话就在静态化的时候用Replace把关键字替换成连接,为了计数,先用IndexOf查找下有没有对应关键字--------------------编程问答-------------------- 我的方法也比较笨,没有考虑到的,可以再做一些判断:
int count=0;//关键字个数
List<string> keylist;//关键字集合
string content;//文章内容
foreach(string str in keylist)
{
if(content.IndexOf(str)>=0)
{
content.Replace(str,"<a href='#'>"+str+"</a>");
count++
}
if(count>=8)
break;
}
1、string contentStr=【文章内容】;//获取数据库信息-文章内容字符串
2、int isHas = contentStr.IndexOf("中国");
int countHas=0;
while(isHas>0&&countHas<8)
{
countHas+=1;
isHas = contentStr.IndexOf("中国",isHas+1);
}
if(countHas<9)
{
contentstr=contentstr.Replace("中国","<a href=' 来自数据库'>中国</a>");
}
else
{
string preStr= contentstr.substring(0,isHas+2);
string backStr=contentstr.substring(isHas+2,contentstr.Length-isHas-2);
preStr=preStr.Replace("中国","<a href=' 来自数据库'>中国</a>");
contentStr=preStr+backStr;
}
3.绑定contentStr --------------------编程问答--------------------
++
问题真的好难!! --------------------编程问答-------------------- 我的思路是这样的 首先 你要把文章中所有的关键字保存起来 然后再用正则表达式进行替换! --------------------编程问答-------------------- 个人觉得 像这种 完全可以在文章内容输入时人为的加入链接,而不是通过代码去实现,即便你匹配成功,那么替换成什么链接,链接地址又是如何?你又怎么去规定,难道所有链接都指向一个死链接地址?这种事情还是交给编辑来做吧. --------------------编程问答--------------------
连接可以指向一个搜索的页面,把要搜索的关键字传过去进行搜索....如果人工加进去,累是一回事,还有可能漏,主要的还是关键字不能动态改变,虽然我不知道动态改变有没有意义 --------------------编程问答-------------------- --------------------编程问答-------------------- 那你可以直接链接到 百度 了,呵呵,但是很少有人会这么去做,我点了链接 最希望看到的是这方面的合理解释,而不是让用户再增加一个搜索的操作. --------------------编程问答--------------------
额...明白你的意思了,你的意思是点了关键字,显示的是对关键字的解释...
我的意思是,点了关键字,显示站内拥有相同关键字的文章
其实按你的意思也是可以实现的,可以在关键字表加上记录连接信息的字段 --------------------编程问答-------------------- ArrayList aLArrayList = DAL.ArticleManage.NewsKeyWord._NewsKeyWord.GetNewsKeyWord();
int intCount = aLArrayList.Count;
string contentStr = Editor1.Text;//获取数据库信息-文章内容字符串
for (int i = 0; i < intCount; i++)
{
Model.ArticleManage.NewsKeyword objKeyWord = (Model.ArticleManage.NewsKeyword)aLArrayList[i];
int isHas = contentStr.IndexOf(objKeyWord.KeyWord);
int countHas = 0;
while (isHas > 0 && countHas < 8)
{
countHas += 1;
isHas = contentStr.IndexOf(objKeyWord.KeyWord, isHas + 1);
}
if (countHas < 9)
{
// contentstr=contentstr.Replace("中国","<a href=' 来自数据库'>中国</a>");
Regex r = new Regex(objKeyWord.KeyWord);
//strReplaceText = strReplaceText.Replace(objKeyWord.KeyWord, " <a href= '" + objKeyWord.URL + "' target=_blank> " + objKeyWord.KeyWord + "</a> ");
contentStr = r.Replace(contentStr, " <a href= '" + objKeyWord.URL + "' target=_blank> " + objKeyWord.KeyWord + "</a> ", 1);
}
else
{
string preStr = contentStr.Substring(0, isHas + 2);
string backStr = contentStr.Substring(isHas + 2, contentStr.Length - isHas - 2);
preStr = preStr.Replace(objKeyWord.KeyWord, " <a href= '" + objKeyWord.URL + "' target=_blank> " + objKeyWord.KeyWord + "</a> ");
contentStr = preStr + backStr;
}
}
//Model.ArticleManage.NewsKeyword objKeyWord = (Model.ArticleManage.NewsKeyword)aLArrayList[i];
// string strReplaceText = Editor1.Text;
// int isHas = contentStr.IndexOf();
//ArrayList aLArrayList=DAL.ArticleManage .NewsKeyWord ._NewsKeyWord .GetNewsKeyWord ();
//int intCount = aLArrayList.Count;
////int intAllCount = 0;
////int intCountReplace = 0;
//for (int i = 0; i < intCount; i++)
//{
// Model.ArticleManage.NewsKeyword objKeyWord = (Model.ArticleManage.NewsKeyword)aLArrayList[i];
//int k = 0;
//string strFind = objKeyWord.KeyWord;
//while (strReplaceText.Length > 0)
////{
//if (strReplaceText.Contains(objKeyWord.KeyWord))
//{
// for (int k = 0; k < intCount;k++ )
// //strReplaceText = strReplaceText.Substring(strReplaceText.IndexOf(objKeyWord.KeyWord)+1);
// if (k <= 2)
// {
//if (Regex.Matches(strReplaceText, objKeyWord.KeyWord).Count < 4)
//{
//intAllCount += Regex.Matches(strReplaceText, objKeyWord.KeyWord).Count;
//if (intAllCount < 3)
//{
//if (strReplaceText.Contains(objKeyWord.KeyWord))
//{
// for (int k = 0,h=0; k < intCount;k++,h++)
// {
// if (h < 2)
// {
// Regex r = new Regex(objKeyWord.KeyWord);
//strReplaceText = strReplaceText.Replace(objKeyWord.KeyWord, " <a href= '" + objKeyWord.URL + "' target=_blank> " + objKeyWord.KeyWord + "</a> ");
// strReplaceText = r.Replace(strReplaceText, " <a href= '" + objKeyWord.URL + "' target=_blank> " + objKeyWord.KeyWord + "</a> ", 1);
// }
// else
// {
// MessageShow("文章的关键字超出限制个数啦!");
// return;
// }
// }
//}
// Regex r = new Regex(objKeyWord.KeyWord);
// //strReplaceText = strReplaceText.Replace(objKeyWord.KeyWord, " <a href= '" + objKeyWord.URL + "' target=_blank> " + objKeyWord.KeyWord + "</a> ");
//strReplaceText = r.Replace(strReplaceText, " <a href= '" + objKeyWord.URL + "' target=_blank> " + objKeyWord.KeyWord + "</a> ", 1);
// string str = "asfdjidfasdfaa ";
//string strHaveReplaceText="f";
//string[] arr=new string[20];
//for (int k = 0; k < str.Length;k++ )
//{
// arr[k] = str.Substring(k,1);
// if (arr[i] == strHaveReplaceText)
// {
// intCountReplace++;
// }
//}
//if (intCountReplace >2)
//{
// MessageShow("文章的关键字超出限制个数啦!");
// return;
// }
// else
// {
// break;
// }
//}
//else
//{
// MessageShow("文章的关键字超出限制个数啦!");
// return;
//}
//}
//this.Editor1.Text = strReplaceText;
补充:.NET技术 , ASP.NET