想用asp.net为关键词加上超链接,C#.net为关键词加上超链接,功能要求:
1、为html文本里的自定义关键词加上超链接
2、关键词出面在html标签属性值,则不加超链接
3、在a或pre开始标签与结束标签内的的不加超链接
4、可以为关键词加上超链接的地方都加上超链接
5、文章里的关键词全都加上同个超链接,不符合搜索引擎优化,所以可以自定义替换次数就最好。
自己写了一个用asp.net为关键词加上超链接,C#.net为关键词加上超链接的方法,大家看看有什么地方可以改进的,想加快速度。有些条件可以没有考虑到,也希望指出。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | /// <summary>   /// 为关键词加上超链接   /// </summary>   /// e.g.:    /// string result=GetInnertLink("<a href="http//www.baidu.com" mce_href="http/www.baidu.com">Ningxi</a>Xi过得<span>XI<span>好<a href="http://www.ningxi.com" mce_href="http://www.ningxi.com">快乐</a>!","xi","ningxi","http://www.ningxi.com","_blank",0)   /// <param name="htmlcode">要把关键词加上超链接的html源文本</param>   /// <param name="keyword">将要加上超链接的关键词</param>   /// <param name="title">将要加上的超链接的描文本</param>   /// <param name="url">将要加上的超链接的url地址</param>   /// <param name="target">将要加上的超链接的打开方式</param>   /// <param name="num">为html文本内的前num个关键词加上超链接,0代表全加上超链接</param>   /// <returns>返回为关键词加上超链接后的html文本</returns>   privatestaticstringGetInnertLink(stringhtmlcode, stringkeyword, stringtitle, stringurl, stringtarget, intnum)   {       stringhtmlcodeResult = htmlcode;  //用于保存最新的html文本     stringhtmlcodeLower = htmlcodeResult.ToLower();  //用于保存最新的Hmtl文本的小写,方便不分大小写找出关键词     stringkeywordResult = "";  //用于保存关键词的原来面貌,可能是大写,或者有大也有小     intkeyIndex = 0;           //关键词所在位置     intcurrentIndex = 0;       //每次搜索关键词的开始位置     intcurrentNum = 0;         //保存当前加上了多少个有效超链接     intLBracketIndex = 0;      //左尖括号<位置     intRBracketIndex = 0;      //右尖括号>位置     if(num == 0)       {           num = htmlcode.Length;       }       while(currentIndex <= htmlcodeLower.Length && currentNum < num)       {           if(htmlcodeLower.IndexOf(keyword.ToLower(), currentIndex) > -1)           {               keyIndex = htmlcodeLower.IndexOf(keyword.ToLower(), currentIndex);               LBracketIndex = keyIndex;               do             {                   LBracketIndex = htmlcodeLower.LastIndexOf("<", LBracketIndex - 1, LBracketIndex - currentIndex);               }               while(LBracketIndex != -1 && htmlcodeLower.Substring(LBracketIndex + 1, 1) == "/");               RBracketIndex = htmlcodeLower.LastIndexOf(">", keyIndex - 1, keyIndex - currentIndex);               if(LBracketIndex <= RBracketIndex)               {                   //不在标签的属性内,可以有在标签开始与结束标志内,或在开始与结束标志外                   LBracketIndex = htmlcodeLower.LastIndexOf("<", keyIndex - 1, keyIndex - currentIndex);                   if(LBracketIndex != -1 && htmlcodeLower.Substring(LBracketIndex + 1, 1) != "/")                   {                       //在开始与结束标志内                       //关键词在开始标签与结束标签内,要再判定是不是a标签或pre标签                       if(htmlcodeLower.Substring(LBracketIndex + 1, 1) == "a"|| htmlcodeLower.Substring(LBracketIndex + 3, 3) == "pre")                       {                           //关键词在开始与结束a标签或pre标签内,不可加超链接,循环再来                           currentIndex = keyIndex + keyword.Length;                       }                       else                     {                           //可以加超链接                           keywordResult = htmlcodeResult.Substring(keyIndex, keyword.Length);                           htmlcodeResult = htmlcodeResult.Remove(keyIndex, keyword.Length);                           htmlcodeResult = htmlcodeResult.Insert(keyIndex, "<a href="+ url + " mce_href="+ url + " title="+ title + " target="+ target + ">"+ keywordResult + "</a>");                           htmlcodeLower = htmlcodeResult.ToLower();                           currentIndex = htmlcodeResult.IndexOf("</a>", keyIndex) + 4;                           currentNum += 1;                       }                   }                   else                 {                       //在结束标志外,可以加超链接                       keywordResult = htmlcodeResult.Substring(keyIndex, keyword.Length);                       htmlcodeResult = htmlcodeResult.Remove(keyIndex, keyword.Length);                       htmlcodeResult = htmlcodeResult.Insert(keyIndex, "<a href="+ url + " mce_href="+ url + " title="+ title + " target="+ target + ">"+ keywordResult + "</a>");                       htmlcodeLower = htmlcodeResult.ToLower();                       currentIndex = htmlcodeResult.IndexOf("</a>", keyIndex) + 4;                       currentNum += 1;                   }               }               else             {                   //关键词是标签内的属性值,不可加超链接,循环再来                   currentIndex = keyIndex + keyword.Length;               }           }           else         {               currentIndex = htmlcodeLower.Length + 1;           }       }       returnhtmlcodeResult;   }   | 
asp.net为关键词加上超链接,C#.net 为关键词加上超链接
原文:http://www.cnblogs.com/robinapp/p/4792317.html