想用asp.net爲關鍵詞加上超連接,C#.net爲關鍵詞加上超連接,功能要求:
一、爲html文本里的自定義關鍵詞加上超連接
二、關鍵詞出面在html標籤屬性值,則不加超連接
三、在a或pre開始標籤與結束標籤內的的不加超連接
四、能夠爲關鍵詞加上超連接的地方都加上超連接
五、文章裏的關鍵詞全都加上同個超連接,不符合搜索引擎優化,因此能夠自定義替換次數就最好。
本身寫了一個用asp.net爲關鍵詞加上超連接,C#.net爲關鍵詞加上超連接的方法,你們看看有什麼地方能夠改進的,想加快速度。有些條件能夠沒有考慮到,也但願指出。
html
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>
private
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 +
" 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;
}
}
return
htmlcodeResult;
}
|