用户操作
[即时聊天] [发私信] [加为好友]
爱你网
爱你网的公告








一个不错的P2P软件,里面肯定有你要的资源

我的爱你网,讨论.Net


最近评论
Kaelzhang:您好,今天无意路过这里,发现阁下转载了本站的日志。可能你是从其他的地方转载的,也有可能是从本站转载的。

但是不论是哪一种情况,请加上本站原文的有效链接,http://yottaworks.net/general/142/
或者
http://www.kael.com.cn

感谢您的支持,遵守Creative Commons,互相……
xiaojiit:能不能支态变宽呢?
z392989029:美女,你也学编程吗。
这个东西我还是不会用
z392989029:美女,你也学编程吗。
这个东西我还是不会用
hhshang:楼主你做过这个打字程序,如果做了,可不可以发一份给我学习一下呀,我的邮箱是:zeng_xianchun@126.com
文章分类
收藏
相册
Csdn专用
个人专用
XML相关资料
XML Server与XML-enabled Web Server介绍
xmlhttp发送 xml 例子详解
在Asp.net里显示XML格式内容.
树的资料
asp.net中的treeview 怎么用
数据库连接
CSDN数据库连接大全
DSN方式连接数据库
杂类
100分问一个突然出现的Cookie问题,关于添加/删除Cookie的。
c#存取图片 (RSS)
我的主页(RSS)
高效.TEXTBLOG技巧终结篇
存档
软件项目交易
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
订阅到BlogLines
订阅到Yahoo
订阅到GouGou
订阅到飞鸽
订阅到Rojo
订阅到newsgator
订阅到netvibes

转载 How to Make Dynamic Hyperlinks Using C# in ASP.NET收藏

新一篇: ajax实现动态从数据库模糊查询显示到下拉框中(ajax方法返回Dataset的例子) | 旧一篇: AjaxPro在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://___FCKpd___0amp;\"" + 
param + ">___FCKpd___0amp;</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;
}
 
 

发表于 @ 2008年04月07日 11:18:00|评论(loading...)|编辑

新一篇: ajax实现动态从数据库模糊查询显示到下拉框中(ajax方法返回Dataset的例子) | 旧一篇: AjaxPro在ASP.NET中的应用简介

评论:没有评论。

发表评论  


当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
Csdn Blog version 3.1a
Copyright © 爱你网