国产精品一品二品_49vv亚洲_亚洲美女激情视频_国产成人亚洲无吗淙合青草_欧美在线天堂_人妻av无码av中文av日韩av_av在线播放免费_91亚洲精品在线观看_韩国 欧美 日产 国产精品_久久人人爽

首頁>> 開發技術知識>> 網站建站知識>>ASP.net 關健詞加鏈接


ASP.net 關健詞加鏈接

/// <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.hjnf.com.cn" 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;
    }

分享聯系我們

掃二維碼與項目經理溝通 我們在微信上24小時期待你的聲音

感謝您的關注分享“ASP.net 關健詞加鏈接”
煙寒網絡真誠為您服務 ,點擊客服直接咨詢下單.

我們服務

南寧網絡公司南寧網站建設

網站建設

高端網站建設服務商、相信品牌力量、相信知名企業;制作后收費13014982176!

南寧微信小程序/微信公眾號

微信小程序/微信公眾號

微信小程序 背靠10億流量,不論用戶需求是什么,都能被開發使用 一點創意,就能在“社交圈”中脫穎而出。

南寧網站優化/SEO

網站優化/SEO

讓您的網站更靠近排名位置,讓客戶更容易找到您,seo優化是一個漫長過程,整個網站營銷花最少的錢起最大的作用。

我們服務客戶


企業形象網站、個人博客、政府門戶網站、新聞傳媒網站最省錢、省時、省備案 建站快、效率高更多服務
廣西華崧集團網站品牌設計

廣西華崧集團網站品牌設計

廣西華崧林業發展集團(以下簡稱華崧集團)組建于2017年8月,集團總部位于廣西壯...

廣西大和平房地產有限公司

廣西大和平房地產有限公司

廣西鴻生源環保科技有限公司

廣西鴻生源環保科技有限公司

主站蜘蛛池模板: 凌云县| 安顺市| 南丰县| 阿拉善盟| 光泽县| 博湖县| 修水县| 万源市| 平南县| 塘沽区| 尚义县| 内江市| 宁河县| 阿城市| 来安县| 梁河县| 上思县| 进贤县| 孙吴县| 漯河市| 酒泉市| 柳林县| 郎溪县| 建宁县| 鄯善县| 凤凰县| 湾仔区| 凤阳县| 台湾省| 九江市| 寿光市| 湘阴县| 静宁县| 兴和县| 开封县| 资溪县| 东兰县| 阜南县| 临泽县| 邢台县| 英山县|