在線客服
24小時免費咨詢電話:18978941786
客服時間:上午9:30~下午6點
ASP.net 關健詞加鏈接
收藏 分享 發布日期:2012-2-12 16:30:51 編輯:admin 文章來源: 點擊率:
/// 為關鍵詞加上超鏈接
/// </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.lidajijin.com" mce_href=)
/// <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>
public static string GetInnertLink(string htmlcode, string keyword, string title, string url, string target, int num)
{
string htmlcodeResult = htmlcode; //用于保存最新的html文本
string htmlcodeLower = htmlcodeResult.ToLower(); //用于保存最新的Hmtl文本的小寫,方便不分大小寫找出關鍵詞
string keywordResult = ""; //用于保存關鍵詞的原來面貌,可能是大寫,或者有大也有小
int keyIndex = 0; //關鍵詞所在位置
int currentIndex = 0; //每次搜索關鍵詞的開始位置
int currentNum = 0; //保存當前加上了多少個有效超鏈接
int LBracketIndex = 0; //左尖括號<位置
int RBracketIndex = 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 + "' title='" + title + "' target='" + target + "'>" + keywordResult + "</a>");
htmlcodeLower = htmlcodeResult.ToLower();
currentIndex = htmlcodeResult.IndexOf("</a>", keyIndex) + 4;
currentNum += 1;
}
}
else if ((RBracketIndex = htmlcodeLower.LastIndexOf(">", keyIndex - 1, keyIndex - currentIndex)) != -1)
{
//
// 當查找范圍內存在'>'標簽則說明在一個靜態控件中則需要判斷這個控件是否是a標簽
//
if (htmlcodeLower.Substring(htmlcodeLower.IndexOf('<', currentIndex) + 1, 2) == "/a")
{
//關鍵詞在a標簽內則不能添加超鏈接
currentIndex = keyIndex + keyword.Length;
}
}
else
{
//在結束標志外,可以加超鏈接
keywordResult = htmlcodeResult.Substring(keyIndex, keyword.Length);
htmlcodeResult = htmlcodeResult.Remove(keyIndex, keyword.Length);
htmlcodeResult = htmlcodeResult.Insert(keyIndex, "<a 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;
}
}
return htmlcodeResult;
} 本文章由南寧網站建設、南寧網站優化、南寧網絡公司整理,轉載請注明出處:http://www.lidajijin.com/