zoukankan      html  css  js  c++  java
  • How to Make Dynamic Hyperlinks Using C# in ASP.NET

    We've all seen blogs and other web sites that seem to be able to intersperse hyperlinks throughout their content, almost whimsically, if the given text is "linkable", or, formatted like a domain name or link of some kind.

    I recently had the need to do this (for my site, nonsequiturs.com), and couldn't find a great source for a starting point, well, in C# anyway. So, I went ahead and made one using regular expressions and simple string replaces. It works well, and is relatively fast and reliable. I thought I'd share the code in case others had the need for this functionality.

    Features: Automatically makes a hyperlink out of anything that appears to be a domain name or URL. Will skip confusing text, like number sequences (e.g., 10.35), and will ignore existing hyperlinks. You can enclose text with a special tag if you want to specify an area that should not be processed (see the code comments for more). You can also add paramaters to the hyperlink tags that are generated, like "target=" and so forth.

    For more code samples, or to contact me directly, visit my site.

    Collapse
    /// <summary>
    /// Convert domain names and url paths to real web links.
    /// Handles the most common web transport protocols.
    /// Existing <a> tag contents are ignored. Any valid urls
    /// within a <nolink></nolink> tag are ignored.
    /// </summary>
    /// <param name="strvar">String to process.</param>
    /// <param name="param">String of parameters to insert into
    /// the resultant <a> tags, like target="_blank".</param>
    /// <returns>A string with links</returns>
    public static string AutoHyperlinks(string strvar, string param)
    {
    // (c)2006 Michael Argentini, http://www.nonsequiturs.com.
    //
    // Please keep this copyright intact.
    // You may use or modify this code however you see fit,
    // within the scope of application or web site functionality.
    // Distribution of this code as an example or snippet is
    // prohibited. In this case, please link to the code example
    // on the nonsequiturs.com site directly!
    // First, process all <nolink> areas and change period
    // characters temporarily to avoid auto-hyperlink processing.
    string final = strvar;
    Regex regex = new Regex(@"<nolink>(.*?)</nolink>",
    RegexOptions.IgnoreCase | RegexOptions.Singleline |
    RegexOptions.CultureInvariant |
    RegexOptions.IgnorePatternWhitespace |
    RegexOptions.Compiled);
    MatchCollection theMatches = regex.Matches(strvar);
    for (int index = 0; index < theMatches.Count; index++)
    {
    final = final.Replace(theMatches[index].ToString(),
    theMatches[index].ToString().Replace(".", "[[[pk:period]]]"));
    }
    // Second, process all existing <a> tags and change period
    // characters in them temporarily to avoid auto-hyperlink processing.
    regex = new Regex(@"<a(.*?)</a>", RegexOptions.IgnoreCase |
    RegexOptions.Singleline | RegexOptions.CultureInvariant |
    RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);
    theMatches = regex.Matches(final);
    for (int index = 0; index < theMatches.Count; index++)
    {
    final = final.Replace(theMatches[index].ToString(),
    theMatches[index].ToString().Replace(".", "[[[pk:period]]]"));
    }
    // Third, temporarily alter any digit sequences
    // that are formatted like domain names.
    final = Regex.Replace(final, @"(?<=\d)\.(?=\d)", "[[[pk:period]]]");
    // Fourth, look for, and process, any linkable domain names or URLs.
    Regex tags = new Regex(@"([a-zA-Z0-9\:/\-]*[a-zA-Z0-9\-_]\" +
    @".[a-zA-Z0-9\-_][a-zA-Z0-9\-_][a-zA-Z0-9\?\" +
    @"=&#_\-/\.]*[^<>,;\.\s\)\(\]\[\""])");
    // Fifth, fix any inadvertently altered protocol strings.
    final = tags.Replace(final, "<a href=\"http://$&\"" + 
    param + ">$&</a>");
    final = final.Replace("http://https://", "https://");
    final = final.Replace("http://http://", "http://");
    final = final.Replace("http://ftp://", "ftp://");
    final = final.Replace("http://rtsp://", "rtsp://");
    final = final.Replace("http://mms://", "mms://");
    final = final.Replace("http://pcast://", "pcast://");
    final = final.Replace("http://sftp://", "sftp://");
    final = final.Replace("[[[pk:period]]]", ".");
    final = final.Replace("<nolink>", "");
    final = final.Replace("</nolink>", "");
    // Lastly, return the processed string.
    return final;
    }
    
    
    
  • 相关阅读:
    九度 1502:最大值最小化(二分法)
    九度 1499:项目安排(任务调度, 01背包变形)
    九度 1498:寻找表达式(暴力搜索, 计算表达式)
    正则——只能允许是汉字、拼音和数字的正则表达式
    vs出现“已经在解决方案中打开了具有该名称的项目”问题的解决方案
    DNS劫持解决方法
    Unity3D 面试题汇总
    MonoDevelop几个常用的快捷键
    《要死就一定要死在你手里》——虐心歌曲
    美国队长2-冬日战士
  • 原文地址:https://www.cnblogs.com/goody9807/p/970153.html
Copyright © 2011-2022 走看看