国产高清一区二区在线_免费看肥胖女人做爰高清全过情_日韩少妇高潮抽搐_性生生活大片又黄又_粉嫩蜜臀av国产精品网站_搞黄视频免费_亚洲成a人一区二区三区_屁股翘起来趴好挨c_少妇一夜爽免费看_日本护士高潮大叫

在線客服:  

「南寧煙寒網(wǎng)絡(luò)」提供南寧網(wǎng)站建設(shè)、網(wǎng)站策劃、南寧網(wǎng)頁(yè)制作、網(wǎng)站設(shè)計(jì)、網(wǎng)站改版、南寧SEO優(yōu)化、網(wǎng)站維護(hù)、南寧網(wǎng)站優(yōu)化、南寧網(wǎng)站推廣、廣告設(shè)計(jì)等服務(wù)..

咨詢熱線

在線客服

24小時(shí)免費(fèi)咨詢電話:18978941786

客服時(shí)間:上午9:30~下午6點(diǎn)

當(dāng)前位置:首頁(yè)>> 技術(shù)文章 >> NET學(xué)習(xí) >> ASP.net 關(guān)健詞加鏈接

ASP.net 關(guān)健詞加鏈接

收藏 分享 發(fā)布日期:2012-2-12 16:30:51    編輯:admin  文章來(lái)源:  點(diǎn)擊率:

/// <summary> 
    /// 為關(guān)鍵詞加上超鏈接 
    /// </summary> 
    /// e.g.:  
    /// string result=GetInnertLink("<a href="http//www.baidu.com" mce_href="http/www.baidu.com">Ningxi</a>Xi過(guò)得<span>XI<span>好<a href="http://m.absorbed3d.com" mce_href=
    /// <param name="htmlcode">要把關(guān)鍵詞加上超鏈接的html源文本</param> 
    /// <param name="keyword">將要加上超鏈接的關(guān)鍵詞</param> 
    /// <param name="title">將要加上的超鏈接的描文本</param> 
    /// <param name="url">將要加上的超鏈接的url地址</param> 
    /// <param name="target">將要加上的超鏈接的打開(kāi)方式</param> 
    /// <param name="num">為html文本內(nèi)的前num個(gè)關(guān)鍵詞加上超鏈接,0代表全加上超鏈接</param> 
    /// <returns>返回為關(guān)鍵詞加上超鏈接后的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文本的小寫(xiě),方便不分大小寫(xiě)找出關(guān)鍵詞
        string keywordResult = "";  //用于保存關(guān)鍵詞的原來(lái)面貌,可能是大寫(xiě),或者有大也有小
        int keyIndex = 0;           //關(guān)鍵詞所在位置
        int currentIndex = 0;       //每次搜索關(guān)鍵詞的開(kāi)始位置
        int currentNum = 0;         //保存當(dāng)前加上了多少個(gè)有效超鏈接
        int LBracketIndex = 0;      //左尖括號(hào)<位置
        int RBracketIndex = 0;      //右尖括號(hào)>位置
        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)
                {
                    //不在標(biāo)簽的屬性內(nèi),可以有在標(biāo)簽開(kāi)始與結(jié)束標(biāo)志內(nèi),或在開(kāi)始與結(jié)束標(biāo)志外 
                    LBracketIndex = htmlcodeLower.LastIndexOf("<", keyIndex - 1, keyIndex - currentIndex);
                    if (LBracketIndex != -1 && htmlcodeLower.Substring(LBracketIndex + 1, 1) != "/")
                    {
                        //在開(kāi)始與結(jié)束標(biāo)志內(nèi) 
                        //關(guān)鍵詞在開(kāi)始標(biāo)簽與結(jié)束標(biāo)簽內(nèi),要再判定是不是a標(biāo)簽或pre標(biāo)簽 
                        if (htmlcodeLower.Substring(LBracketIndex + 1, 1) == "a" || htmlcodeLower.Substring(LBracketIndex + 3, 3) == "pre")
                        {
                            //關(guān)鍵詞在開(kāi)始與結(jié)束a標(biāo)簽或pre標(biāo)簽內(nèi),不可加超鏈接,循環(huán)再來(lái) 
                            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)
                    {
                        //
                        // 當(dāng)查找范圍內(nèi)存在'>'標(biāo)簽則說(shuō)明在一個(gè)靜態(tài)控件中則需要判斷這個(gè)控件是否是a標(biāo)簽
                        //
                        if (htmlcodeLower.Substring(htmlcodeLower.IndexOf('<', currentIndex) + 1, 2) == "/a")
                        {
                            //關(guān)鍵詞在a標(biāo)簽內(nèi)則不能添加超鏈接
                            currentIndex = keyIndex + keyword.Length;
                        }
                    }
                    else
                    {
                        //在結(jié)束標(biāo)志外,可以加超鏈接 
                        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
                {
                    //關(guān)鍵詞是標(biāo)簽內(nèi)的屬性值,不可加超鏈接,循環(huán)再來(lái) 
                    currentIndex = keyIndex + keyword.Length;
                }
            }
            else
            {
                currentIndex = htmlcodeLower.Length + 1;
            }
        }
        return htmlcodeResult;
    } 本文章由
南寧網(wǎng)站建設(shè)、南寧網(wǎng)站優(yōu)化、南寧網(wǎng)絡(luò)公司整理,轉(zhuǎn)載請(qǐng)注明出處:http://m.absorbed3d.com/

關(guān)于我們 | 域名主機(jī) | 建站套餐 | 企業(yè)動(dòng)態(tài) | 成功案例 | 網(wǎng)站推廣 | 建站知識(shí) | 常見(jiàn)問(wèn)題 | 聯(lián)系我們

南寧煙寒網(wǎng)絡(luò)竭誠(chéng)為您免費(fèi)提供南寧網(wǎng)站建設(shè)、南寧網(wǎng)站設(shè)計(jì)、南寧網(wǎng)站優(yōu)化、維護(hù)以及網(wǎng)站技術(shù)很方面的網(wǎng)絡(luò)服務(wù)!

南寧網(wǎng)站建設(shè)、南寧網(wǎng)絡(luò)公司咨詢熱線電話:0771-5306126 18967841786(24小時(shí)全天電話)

煙寒網(wǎng)絡(luò) - 讓你進(jìn)一步走向成功