码迷,mamicode.com
首页 > 其他好文 > 详细

C#自动给文章关键字加链接实现代码

时间:2014-05-31 12:45:40      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

bubuko.com,布布扣
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ivony.Html;
using Ivony.Html.Parser;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<ContModel> strarr = new List<ContModel>();
        strarr.Add(new ContModel() { key = "网络", Url = "http://baidu.com" });
        strarr.Add(new ContModel() { key = "QQ", Url = "http://qq.com" });
        strarr.Add(new ContModel() { key = "中国", Url = "http://baidu.com" });
        strarr.Add(new ContModel() { key = "知识", Url = "http://baidu.com" });
        string str = " 知识技术网络;网络应用 网络基本知识 <p><img src=‘/sss.jpg‘ alt=‘网络技术网络;网络应用‘/>中國<a href=‘http://gg.com‘>44</a></p><a  href=\"http://www.stobar.cn\">网络技术网络;网络应用 网络基本知识</a>";
        foreach (var s in strarr)
        {
            str = GetInnertLink(str, s.key, s.key, s.Url, "_blank", 1);
        }
        // string result = GetInnertLink(str, "网络", "网络", "http:baidu.com", "rr", 1);
        Response.Write(str);
        Response.Redirect("");
    }




    /// <summary>
    /// 为关键词加上超链接
    /// </summary>
    /// e.g.: 
    /// string result=GetInnertLink(htmlcode,"xi","ningxi","http://hi.csdn.net/ningxi_","_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>
    public static string GetInnertLink(string htmlcode, string keyword, string title, string url, string target, int num)
    {
        string htmlcodeResult = htmlcode;
        string htmlcodeLower = htmlcodeResult.ToLower();
        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);
                    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 title=" + title + "  href=" + url + " mce_href=" + url + "  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 title=" + title + "  href=" + url + " mce_href=" + url + "  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;
    }


}
public class ContModel
{
    public string key { get; set; }
    public string Url { get; set; }
}
bubuko.com,布布扣

 

C#自动给文章关键字加链接实现代码,布布扣,bubuko.com

C#自动给文章关键字加链接实现代码

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/gzalrj/p/3761250.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!